问题
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