Qt 在两个 QListWidget 之间拖放
我有两个 QListWidget(list1 和 list2)
list1
应该能够从list2
接收项目list1
应该能够使用内部重新组织拖放list2
应该能够从list1
接收项目,
list1->setSelectionMode(QAbstractItemView::SingleSelection);
list1->setDragEnabled(true);
list1->setDragDropMode(QAbstractItemView::DragDrop);
list1->viewport()->setAcceptDrops(true);
list1->setDropIndicatorShown(true);
ulist2->setSelectionMode(QAbstractItemView::SingleSelection);
list2->setDragEnabled(true);
list2->setDragDropMode(QAbstractItemView::InternalMove);
list2->viewport()->setAcceptDrops(true);
list2->setDropIndicatorShown(true);
我必须将 list2
放在 InternalMove
上,否则当我将项目拖到 list1
时,项目并未被删除。
如果我将 list1
放入 InternalMove
中,我就不能再在上面放任何东西了。
我是否必须编写自己的拖放功能才能做到这一点?
I've two QListWidget (list1 and list2)
list1
should be able to receive items fromlist2
list1
should be able to be reorganized with an internal drag and droplist2
should be able to receive items fromlist1
list1->setSelectionMode(QAbstractItemView::SingleSelection);
list1->setDragEnabled(true);
list1->setDragDropMode(QAbstractItemView::DragDrop);
list1->viewport()->setAcceptDrops(true);
list1->setDropIndicatorShown(true);
ulist2->setSelectionMode(QAbstractItemView::SingleSelection);
list2->setDragEnabled(true);
list2->setDragDropMode(QAbstractItemView::InternalMove);
list2->viewport()->setAcceptDrops(true);
list2->setDropIndicatorShown(true);
I had to put the list2
on InternalMove
otherwise the item is not remove when I drag it to the list1
.
And if i put list1
to InternalMove
i can't drop any more on it.
Do I have to write my own drag and drop function to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以扩展
QListWidget
覆盖dragMoveEvent
方法如下所示在我们的实现中,我们检查拖动事件,我们不接受(允许)删除来自我们的小部件本身的项目。
如果您使用 QtDesigner 您可以使用当您右键单击表单上的
QListWidget
时,会从上下文菜单中选择“升级到...”选项。您必须输入新类的名称(在我的示例中为MyListWidget
),并且必须输入新头文件的名称,您的类将在其中声明(您可以复制并粘贴上面的代码到这个文件中)。You can extend
QListWidget
overridingdragMoveEvent
method like belowInside our implementation we check the source of the drag event and we don't accept (allow) dropping items that come from our widget itself.
If you're using QtDesigner you can use Promote to... option from the context menu when you right click on the
QListWidget
on your form. You have to enter a name of your new class (MyListWidget
in my example) and you have to enter a name of new header file, where your class will be declared (you can copy and paste the code above into this file).