Qt要启用自定义模型,需要做些什么? [英] Qt what needs to be done for a custom model to enable drop?

查看:89
本文介绍了Qt要启用自定义模型,需要做些什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我已连接到QTreeView的自定义模型上启用放置.

I'm trying to enable drop on a custom model I have hooked up to QTreeView.

我已完成以下操作:

  • 确保在QTreeView
  • 上启用了acceptDrops
  • 在我的自定义模型supportedDropActions上实现为return Qt::CopyAction | Qt::MoveAction
  • 在我的自定义模型mimeType上实现,以返回带有文本/uri-list的QStringList
  • 在我的自定义模型dropMimeData上进行了处理,以处理掉落(如果发生的话).
  • 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.

这就是我需要在QTreeWidget上运行它的全部条件.

This is all I needed to get it working on a QTreeWidget.

我继续:

  • return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
  • 实施的标志
  • Implemented flags to return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;

尽管如此,当我运行我的应用程序并尝试将Windows中的文件拖到我的QTreeView上时,我只是得到了不允许的带有斜线的Ghostbusters风格的圆圈.

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?

推荐答案

最后我自己弄清楚了这一点.

Finally figured this one out on my own.

原来的问题是在flags函数中,在无效索引的情况下,我未能返回ItemIsDropEnabled标志.无效索引是用于根节点的信号,即视图中没有项目的区域,因此空白区域不可放置.

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.

这很难被发现,因为我一直在尝试使用drop将项添加到我的树中,因此没有任何东西可以不进行drop工作,这意味着我所看到的只是带有斜线的圆圈.

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要启用自定义模型,需要做些什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆