开发者

Reordering items in a QTreeWidget with Drag and Drop in PyQt

开发者 https://www.devze.com 2023-04-11 04:58 出处:网络
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

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 o开发者_运维问答ne 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!


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)

For more details, see: Qt.ItemFlags

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号