Reordering items in a QTreeWidget with Drag and Drop in PyQt

梦想与她 提交于 2019-12-12 10:08:41

问题


I have a QTreeWidget with what I thought all the proper settings setup in order to be able to reorder items by dragging them around. It can work, at times, but more often than not I drag an item into another one and it either disappears or becomes a child.

Is there anyway to prevent this from happening so you don't lose items you're trying to reorder? I figured you could achieve this within Qt Designer. I have dragDrop mode set to InternalMove and defaultDropAction set to MoveAction, but I'm not even certain of both of those are what I need to be adjusting.

Thanks in advance!


回答1:


You can add flags to individual tree widget items that control drag and drop behaviour (amongst other things). For instance, to prevent an item being a drop target, try something like this:

item = QTreeWidgetItem(parent)
item.setFlags(items.flags() & ~Qt.ItemIsDropEnabled)

See the qt docs for more details: http://doc.qt.nokia.com/4.7/qt.html#ItemFlag-enum



来源:https://stackoverflow.com/questions/7666823/reordering-items-in-a-qtreewidget-with-drag-and-drop-in-pyqt

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