Python PyQt5:QListWidget不';我不接受滴水

Python PyQt5:QListWidget不';我不接受滴水,python,drag-and-drop,qt5,pyqt5,qlistwidget,Python,Drag And Drop,Qt5,Pyqt5,Qlistwidget,我已经尝试创建一个简单的QListWidget,它接受放入其中的文本。没法去上班。drop事件甚至不会被触发,而drag事件则会被触发。 谁能给我指出正确的方向吗?我做错了什么 提前谢谢 import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QLabel, QListWidget from PyQt5.QtGui import QIcon from PyQt5.QtCore i

我已经尝试创建一个简单的QListWidget,它接受放入其中的文本。没法去上班。drop事件甚至不会被触发,而drag事件则会被触发。 谁能给我指出正确的方向吗?我做错了什么

提前谢谢

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton,  QLineEdit, QLabel, QListWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 drag and drop'
        self.left = 500
        self.top = 400
        self.width = 400
        self.height = 250
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        editBox = QLineEdit('Drag this', self)
        editBox.setDragEnabled(True)
        editBox.move(10, 10)
        editBox.resize(100,32)

        listwidget = CustomLabel(self)
        listwidget.move(130,15)

        self.show()


class CustomLabel(QListWidget):

    def __init__(self, parent):
        super().__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        if e.mimeData().hasFormat('text/plain'):
            print("dragged")
            e.accept()
        else:
            e.ignore()


    def dropEvent(self, e):
        print("dropped")

        self.addItem(event.mimeData().text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

看起来您的代码是基于

主要区别在于
CustomLabel
继承自
QListWidget
,而不是
QLabel
。不幸的是,
QListWidget
继承自
QAbstractScrollArea
,接收各种拖放事件的是与该滚动区域关联的视口小部件,而不是
QListWidget
本身

最好是在视口上安装事件过滤器

class CustomLabel(QListWidget):
    def __init__(self, parent):
        super().__init__(parent)
        self.setAcceptDrops(True)

        # Install the event filter.
        self.viewport().installEventFilter(self)

    def dragEnterEvent(self, e):
        if e.mimeData().hasFormat('text/plain'):
            print("dragged")
            e.accept()
        else:
            e.ignore()

    def eventFilter (self, obj, event):
        if obj == self.viewport():
            print("event")
            if event.type() == QEvent.DragMove:
                print("moved")
                event.accept()

                # Your drag enter event processing code goes here
                return True
            if event.type() == QEvent.Drop:
                print("dropped")
                event.accept()

                # Your drop event processing code goes here
                return True
        return super(CustomLabel, self).eventFilter(obj, event)
编辑1:

您可能还需要添加

from PyQt5.QtCore import QEvent

默认情况下,
QListWidget
不处理删除的文本,因此您必须重新实现mime数据处理,如下所示:

class CustomLabel(QListWidget):    
    def __init__(self, parent):
        super().__init__(parent)
        self.setAcceptDrops(True)

    def mimeTypes(self):
        mimetypes = super().mimeTypes()
        mimetypes.append('text/plain')
        return mimetypes

    def dropMimeData(self, index, data, action):
        if data.hasText():
            self.addItem(data.text())
            return True
        else:
            return super().dropMimeData(index, data, action)
太棒了。现在它起作用了。 非常感谢你的帮助。 这就是全部代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget,  QLineEdit, QListWidget


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 drag and drop'
        self.left = 500
        self.top = 400
        self.width = 400
        self.height = 250
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        editBox = QLineEdit('Drag this', self)
        editBox.setDragEnabled(True)
        editBox.move(10, 10)
        editBox.resize(100,32)

        listwidget = CustomList(self)
        listwidget.move(130,15)

        self.show()


class CustomList(QListWidget):

    def __init__(self, parent):
        super().__init__(parent)
        self.setAcceptDrops(True)

    def mimeTypes(self):
        mimetypes = super().mimeTypes()
        mimetypes.append('text/plain')
        return mimetypes

    def dropMimeData(self, index, data, action):
        if data.hasText():
            self.addItem(data.text())
            return True
        else:
            return super().dropMimeData(index, data, action)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

你需要以某种方式开始拖动。看一看.Dont get it…Sorry,它只告诉特定的小部件接受拖放操作——它与实际启动拖放操作无关。请阅读我第一条评论中链接到的文档。我必须说,阅读文档对我来说并没有什么启发。我不能完全理解那里的代码,因为到目前为止我只熟悉python。你是对的,它只是对该示例的一个稍加修改的版本。我忘记了将类从CustomLabel重命名为其他内容。但是当我用您的代码替换CustomLabel类时,应用程序不会启动/没有显示窗口。请参见编辑1。