Qt what needs to be done for a custom model to enable drop?

怎甘沉沦 提交于 2019-12-22 10:37:09

问题


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 to return Qt::CopyAction | Qt::MoveAction
  • Implemented on my custom model mimeTypes to return a QStringList 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!