dropEvent未在PyQt5中触发

问题描述 投票:0回答:2

我正在尝试在我的应用程序中实现拖放,但是从不调用目标小部件中的dropEvent

我搜索了很多这个问题,但是我发现的每个解决方案都涉及重写dragMoveEvent,我做到了,但没有区别。

由于上述原因,我的示例代码也不起作用:

主窗口类:

class Win(QtWidgets.QWidget):
    def __init__(self):
        super(Win, self).__init__()
        self.setGeometry(200, 300, 400, 200)
        self.setLayout(QtWidgets.QHBoxLayout())
        self.layout().addWidget(DragLabel())
        self.layout().addWidget(DropTest())

要拖动的标签:

class DragLabel(QtWidgets.QLabel):
    def __init__(self):
        super(DragLabel, self).__init__()
        self.setText("Drag me")

    def mouseMoveEvent(self, e):
        if e.buttons() != QtCore.Qt.LeftButton:
            return

        mimeData = QtCore.QMimeData()
        mimeData.setText("Test drop")

        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)

        dropAction = drag.exec(QtCore.Qt.CopyAction)

要放到的小部件:

class DropTest(QtWidgets.QWidget):
    def __init__(self):
        super(DropTest, self).__init__()
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        print("DragEnter")
        e.accept()

    def dragMoveEvent(self, e):
        print("DragMove")
        e.accept()

    def dropEvent(self, e):
        print("DropEvent")
        position = e.pos()
        print(position)
        e.accept()

[当我将标签拖动到另一个小部件上时,我看到dragEnterEventdragMoveEvent都在被调用,但是当我实际上放下标签时,dropEvent函数没有消息。

此外,关闭窗口后,应用程序将挂起并且不会退出。

我正在Fedora 31中使用DNF安装的PyQt 5.13.1x86_64。Python版本是3.7.5,没有virtualenv。

python python-3.x pyqt drag-and-drop pyqt5
2个回答
1
投票

[如评论中所述,我已经在this post中回答了相同的问题,我已经在使用fedora31的docker中对其进行了测试,并且它可以正常工作,因此在与OP讨论后,他在comment中指出了该问题:

[PyQt5]以前是从DNF,但我敢肯定setuptools也已通过pip将其安装为我的应用程序的依赖性。

问题的原因是OP结合了两种安装方式:dnf和pip,它们各自使用不同版本的Qt,编译标志等进行编译,这可能导致某些功能失败。解决方案是通过两种方法都卸载PyQt5,然后仅重新安装其中一种。


0
投票

尝试:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class DropTest(QtWidgets.QLabel):                       # - QWidget    + QLabel
    def __init__(self):
        super(DropTest, self).__init__()
        self.setAcceptDrops(True)
        self.setText(" Accept Drops")
        self.setStyleSheet("QLabel { background-color : #ccd; color : blue; font-size: 20px;}")

    def dragEnterEvent(self, e):
#        print("DragEnter")
        e.accept()

    def dragMoveEvent(self, e):
#        print("DragMove")
        e.accept()

    def dropEvent(self, e):
#        print("DropEvent")
#        position = e.pos()
#        print(position)

        self.setText(e.mimeData().text())                #  +++
        e.setDropAction(Qt.MoveAction)                   #  +++

        e.accept()


class DragLabel(QtWidgets.QLabel):
    def __init__(self):
        super(DragLabel, self).__init__()
        self.setText("Drag me")

    def mouseMoveEvent(self, e):
        if e.buttons() != QtCore.Qt.LeftButton:
            return
        mimeData = QtCore.QMimeData()
        mimeData.setText(self.text())                     #   ("Test drop")
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        dropAction = drag.exec(QtCore.Qt.CopyAction)


class Win(QtWidgets.QWidget):
    def __init__(self):
        super(Win, self).__init__()
        self.setGeometry(200, 300, 400, 200)

        self.setLayout(QtWidgets.QHBoxLayout())
        self.layout().addWidget(DragLabel())
        self.layout().addWidget(DropTest())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Win()
    w.show()
    sys.exit(app.exec_())        

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.