不是顶级小部件时如何接收 dropEvent?

How to receive dropEvent when not a top level widget?

编辑:这个问题似乎是 Qt 5.12.0 版特有的。有关详细信息和解决方法,请参阅答案

我正在尝试实现用于将文件加载到我的应用程序中的拖放区。 当我将小部件显示为顶级小部件时它可以工作,但是一旦我将它包含到另一个父小部件中它就会停止工作。

问题是,尽管我收到了 dragEnterEvent 并接受了它,但我从未看到 dropEvent

这是我的小部件:

class FileDropZone(qt.QLabel):
    """Target area for a drag and drop operation"""
    height = 33

    def __init__(self, text="Add file", parent=None):
        super().__init__(text, parent)
        stylesheet = """
            QLabel {
                border: 2px dotted #B4BDBA;
                qproperty-alignment: AlignCenter;
            }
            """
        self.setStyleSheet(stylesheet)
        self.setAcceptDrops(True)
        self.setFixedHeight(self.height)

    def dragEnterEvent(self, event):
        print("in drag enter event")
        if event.mimeData().hasUrls():
            print("hasUrls()")
            event.acceptProposedAction()

    def dropEvent(self, event):
        print("in drop event")
        urls = event.mimeData().urls()
        for url in urls:
            print(url.isLocalFile(), url.toLocalFile())

这就是我设法让它发挥作用的方法:

app = qt.QApplication([])
a = FileDropZone()
a.show()
app.exec_()

这是一个不起作用的例子(dragEnter 起作用,两个打印件都正确打印,但 dropEvent 不打印任何东西):

app = qt.QApplication([])
a0 = qt.QWidget()
l = qt.QHBoxLayout(a0)
a1 = FileDropZone("drop here", a0)
l.addWidget(a1)
a0.show()
app.exec_()

有什么东西坏了的线索吗?家长是否需要转发事件,如果需要,应该如何实现?

看起来这是 Qt 5.12.0 中引入的错误,将在 Qt 5.12.1 中修复,参见 this discussion and this bug report

同时:

The problem can be worked around by reimplementing dragMoveEvent() and accepting the event there too.

即添加例如FileDropZone class:

下面的方法
def dragMoveEvent(self, event):
    print("in drag move event")
    if event.mimeData().hasUrls():
        print("hasUrls()")
        event.acceptProposedAction()