[Qt-interest] Drag & Drop with custom Model in QTreeView
Sebastian Fischer
creat at gmx.net
Sun Dec 14 20:47:15 CET 2008
>on 14.12.2008 at 08:55 you wrote:
>>2008/12/14 Sebastian Fischer <creat at gmx.net>
>>Now for the actual issue:
>>I have a very confusing problem with Drag & Drop. I have reimplemented
>>a tree model based on QAbstractItemModel and use the default
>>QTreeView to display the contents. It has 3 columns, but only the
>>first column has the Qt::ItemIsSelectable flag set. I want to be able
>>to move items to a new location.
>>Now everything works fine if I set the QTreeView's selectionBehavior
>>to SelectItems EXCEPT the moved items aren't removed (reading a bit of
>>Qt code it turns out that removeRows is only called if complete rows
>>are selected).
>
>This sounds like a reasonable behavior to me. To remove the complete row, you can just reimplement the Drag&Drop behavior by creating a QDrag yourself, and delete the row when the drag succeed.
>
>Benjamin
Yes, it makes perfect sense that a full row has to be dragged in order
to be removed afterwards. I may have been a bit unclear on what my
actual problem is within that rather long mail In the ende I want to
be able to drag&drop lines but only be able to 'grab' items in column
0. The part you have quoted I'd say is correct and the intended
behavior. I've only tried setting selectionBehavior to SelectItems
though since it seems broken for SelectRows.
I'll try to explain with an example.
Let's assume you have the following items in the tree: SelectionMode
is always set to SingleSelection, DragDropMode is set to InternalMove.
SelectionBehavior is set to SelectRows since only then the problem
occurs.
-------------------------------------------------------------
Column 0 Column 1 Column 2
-------------------------------------------------------------
Item 1 item1_attribute1 item1_attribute1
+ Child 1 child1_attribute1 child1_attribute2
+ Child 2 child2_attribute1 child2_attribute2
Item 2 item2_attribute1 item2_attribute2
-------------------------------------------------------------
All Item/Child entries have the following flags set: enabled,
selectable, editable, drag, drop
All attribute entries have only the 'enabled' following flag set (but
the behaviour doesn't change change if all attribute entries have the
same flags set as the Item/Child entries).
I can now drag Item 2 onto Item 1 and it correctly become a child of
it (and with that a sibling of Child 1 and Child 2).
I can NOT drag Child 1 or Child 2!
I can drag Item 1 onto Item 2 and again it correctly becomes a child
of it (Child 1 and Child 2 are still children of Item 1, which is now
a child of Item 2). Everthing fine so far.
After this I can also no longer drag Item 1, even though it hasn't
changed. For some reason only direct children of the invisible root
can be dragged.
As I've already mentioned in my original mail I've tracked the problem
down to inside the startDrag function (implemented in
QAbstractItemView) where the list returned by selectedIndexes() is
empty. As far as I can tell that isn't supposed to happen when I start
dragging an entry (which either was already selected or got selected
by that action)!
Bye,
Sebastian Fischer
More information about the Qt-interest-old
mailing list