简体   繁体   English

PySide2 QTreeWidget拖放项在Mac上消失,Python2.7

[英]PySide2 QTreeWidget drag and drop item disappears on Mac, Python2.7

I'm having a problem where a simple QTreeWidget drag and drop of items causes another item to disappear from the tree.我遇到一个问题,一个简单的 QTreeWidget 拖放项目会导致另一个项目从树中消失。 This is on Mac Monterey 12.4, using Python 2.7 and PySide2.这是在 Mac Monterey 12.4 上,使用 Python 2.7 和 PySide2。 I swear that it had been previously working on a Windows platform, although it's been a while since I've tested that.我发誓它以前一直在 Windows 平台上工作,尽管我已经有一段时间没有测试过了。 I am also having problems with any widgets that had been added to the item getting lost when it re-creates the item in it's new place, so bonus points if anyone knows how to prevent that as well and can show a simple example.我也遇到了任何已添加到该项目的小部件在它的新位置重新创建该项目时丢失的问题,所以如果有人知道如何防止这种情况并且可以显示一个简单的示例,则可以加分。

(I'm working in Maya, so there is some code for displaying the UI there, and I haven't tested the section that would be more generic for use outside of Maya.) (我在 Maya 中工作,所以有一些代码可以在那里显示 UI,我还没有测试过在 Maya 之外使用更通用的部分。)

Thanks so much for any help!...非常感谢您的帮助!...

import sys
from PySide2 import QtCore, QtWidgets


def main():
    try:
        app = QtWidgets.QApplication(sys.argv)
        ui = TreeUI()
        ui.show()
        app.exec_()
    except RuntimeError:
        from maya import OpenMayaUI as omui
        try:
            import shiboken2 as shiboken
        except ImportError:
            import shiboken
        pointer = omui.MQtUtil.mainWindow()
        win = shiboken.wrapInstance(long(pointer), QtWidgets.QWidget)
        ui = TreeUI(parent=win)
        ui.show()


class Tree(QtWidgets.QTreeWidget):
    def __init__(self, parent=None):
        super(Tree, self).__init__(parent)
        self.setHeaderLabels(('name', 'widget'))
        self.setSelectionMode(self.SingleSelection)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setDragDropMode(self.InternalMove)


class TreeUI(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(TreeUI, self).__init__(parent)
        widget = QtWidgets.QWidget(self)
        self.setCentralWidget(widget)
        tree = Tree()
        for x in range(0, 6):
            item = QtWidgets.QTreeWidgetItem(tree, ('item{}'.format(x), None))
            item.setFlags(item.flags() & ~QtCore.Qt.ItemIsDropEnabled)
            button = QtWidgets.QPushButton('Button{}'.format(x))
            tree.setItemWidget(item, 1, button)
        layout = QtWidgets.QVBoxLayout(widget)
        layout.addWidget(tree)


main()

Ah hah!啊哈哈! I got it working, thanks to @musicamante, I was able to get it working.我得到了它的工作,感谢@musicamante,我能够让它工作。 Adding in a custom dropEvent that calls the dropMimeData function, and then accepts the event, seems to fix it up:添加一个调用 dropMimeData 函数的自定义 dropEvent,然后接受该事件,似乎可以修复它:

class Tree(QtWidgets.QTreeWidget):

    def __init__(self, parent=None):
        super(Tree, self).__init__(parent)
        self.setHeaderLabels(('name', 'widget'))
        self.setSelectionMode(self.SingleSelection)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setDragDropMode(self.InternalMove)

    def dropEvent(self, event):
        index = self.indexAt(event.pos())
        parent = index.parent()
        self.model().dropMimeData(
            event.mimeData(),
            event.dropAction(),
            index.row(),
            index.column(),
            parent
        )
        event.accept()

It still has the problem of the associated widgets being lost, but I can post that as a separate issue.它仍然存在相关小部件丢失的问题,但我可以将其作为单独的问题发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM