在PyQt5中未触发dropEvent

我正在尝试在我的应用程序中实现拖放,但是从未调用目标小部件中的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。

yongshu2 回答:在PyQt5中未触发dropEvent

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

  

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

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

,

尝试一下:

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

本文链接:https://www.f2er.com/3163334.html

大家都在问