QTreeView: dragging and dropping items with left mouse button?
-
I'm unable to get drag and drop working using the left mouse button in a QTreeView. It does work with RMB. But with the LMB the view just selects multiple items that I drag across.
Here are my tree view properties:
http://i.stack.imgur.com/NT07q.pngSo, how can I get dragging with a left mouse button to work?
P. S. I have confirmed that with the settings depicted above the "Editable tree model" example demonstrates exactly the same behavior.
-
That's why I added "with the settings depicted above". I open the example, change those properties and observe the undesired behavior.
-
That's not enough. The example also provides a custom model. The model should also provide the Qt::ItemIsDragEnabled item flag as needed if you want your items to be draggable
-
This post is deleted!
-
This post is deleted!
-
Found what's breaking the drag and drop. It's my code that deselects an item that was single-clicked:
void CFileListView::mousePressEvent(QMouseEvent *e)` { const QModelIndex itemClicked = indexAt(e->pos()); QTreeView::mousePressEvent(e); if (e->button() == Qt::LeftButton && itemClicked.isValid() && e->modifiers() == Qt::NoModifier) selectionModel()->clearSelection(); }
If I comment out the
clearSelection
call, drag and drop starts working. WithclearSelection
it's as if D&D is not enabled.
No idea what exactly is happening or how to fix this. -
To start a drag operation you need to select an index. What you do is that you clear that selection right after it happened so there's nothing to drag anymore.
-
I see!
Can I somehow start drag manually in themouseMove
event? -
You would need to re-implement it yourself but why do you do that clear in the pressEvent ?
-
This is a file manager application, this TreeView is the main file list view. Selection on click really looks horrible, and it's redundant - that's what the cursor is for (the thin dotted frame around an item). And not only that, but this selection upon click breaks other selection management operations.
-
So, there's no simple way to start dragging a specific item?