Qt 自定义模型需要做什么才能启用 drop?
我正在尝试在已连接到 QTreeView 的自定义模型上启用拖放功能。
我已完成以下操作:
- 确保在
QTreeView
上启用acceptDrops - 在我的自定义模型
supportedDropActions
上实现以返回 Qt::CopyAction | Qt::MoveAction
- 在我的自定义模型
mimeType
上实现,以返回带有 text/uri-list 的QStringList
- 在我的自定义模型
dropMimeData
上实现code> 来处理掉落(如果发生)。
这就是让它在 QTreeWidget
上运行所需的全部内容。
我继续:
- 实现了返回 Qt::ItemIsEnabled | 的标志Qt::ItemIsSelectable | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
然而,当我运行我的应用程序,并尝试将 Windows 中的文件拖到我的 QTreeView
上时,我只是得到了不允许的捉鬼敢死队风格的圆圈,并带有斜杠它。
我还需要做什么才能启用 Drops?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终于我自己解决了这个问题。
事实证明,问题是在 flags 函数中,在索引无效的情况下,我无法返回 ItemIsDropEnabled 标志。无效索引是根节点的信号,即视图中没有项目的区域,因此空白空间不可删除。
这很难检测到,因为我一直在尝试使用 drop 将项目添加到我的树中,因此没有 drop 可以工作,这意味着我看到的只是带有斜线的圆圈。
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.
对于那些面临类似问题的人,我想指出,有必要在支持的放置操作中返回
Qt::CopyAction
。单独使用Qt::MoveAction
是行不通的。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.