问题
I'm trying to enable drop on a custom model I have hooked up to QTreeView
.
I've done the following:
- Ensured that acceptDrops is enabled on the
QTreeView
- Implemented on my custom model
supportedDropActions
toreturn Qt::CopyAction | Qt::MoveAction
- Implemented on my custom model
mimeType
s to return aQStringList
with text/uri-list - Implemented on my custom model
dropMimeData
to handle the drop if it ever occurred.
This is all I needed to get it working on a QTreeWidget
.
I've gone on to:
- Implemented flags to
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
Nevertheless, when I run my app, and attempt to drag a file in Windows onto my QTreeView
, I just get that not allowed Ghostbusters-style circle with a slash through it.
What else do I need to do to enable drops?
回答1:
Finally figured this one out on my own.
Turns out the issue was that in the flags function, in the case of invalid indexes I had failed to return the ItemIsDropEnabled flag. An invalid index is the signal for the root node, i.e. the area of the view where there were no items, and so the empty space was not droppable.
This had been hard to detect because I had been trying to use drop to add items to my tree, and so there were none without drop working, meaning all I saw was the circle with a slash.
回答2:
For those facing similar problems, I want to point out that it is necessary to return Qt::CopyAction
among the supported drop actions. Qt::MoveAction
alone will not work.
来源:https://stackoverflow.com/questions/7355510/qt-what-needs-to-be-done-for-a-custom-model-to-enable-drop