我有两个QListView继承的类,一个SourceListWidgetAcceptListWidget我希望能够将项目从SourceListWidget复制到AcceptListWidget,并且仍然能够通过拖放在AcceptListWidget中对它们进行排序。

在下面的代码中,除了AcceptListWidget之外,其他所有内容均按我希望的方式运行,无法通过拖放进行排序。我也想知道是否有一种方法可以将项目从AcceptListWidget插入到SourceListWidget中,而不是仅将其追加到末尾。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, \
    QVBoxLayout, QListWidget, QAbstractItemView
from PyQt5.QtGui import QDragMoveEvent, QDropEvent
from PyQt5.QtCore import Qt, QMimeData
import typing


class AcceptListWidget(QListWidget):
    def __init__(self):
        super().__init__()
        self.setDefaultDropAction(Qt.MoveAction)
        self.setDragDropMode(QAbstractItemView.InternalMove)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e: QDragMoveEvent) -> None:
        e.accept()

    def dragMoveEvent(self, e: QDragMoveEvent) -> None:
        e.accept()

    def mimeTypes(self) -> typing.List[str]:
        return ['text/json']

    def mimeData(self, indexes) -> QMimeData:
        drag_data = indexes[0].text()
        mime_data = QMimeData()
        mime_data.setText(drag_data)
        return mime_data

    def dropEvent(self, event: QDropEvent) -> None:
        event.setDropAction(Qt.CopyAction) if event.source() != self else event.setDropAction(Qt.MoveAction)
        if event.source() != self:
            self.addItem(event.mimeData().text())



class SourceListWidget(QListWidget):
    def __init__(self):
        super().__init__()
        self.setDragDropMode(QAbstractItemView.DragDrop)

    def dragMoveEvent(self, e: QDragMoveEvent) -> None:
        e.accept() if e.source() != self else e.ignore()

    def mimeTypes(self) -> typing.List[str]:
        return ['text/json']

    def mimeData(self, indexes) -> QMimeData:
        drag_data = indexes[0].text()
        mime_data = QMimeData()
        mime_data.setText(drag_data)
        return mime_data

    def dropMimeData(self, index: int, data: QMimeData, action: Qt.DropAction) -> bool:
        print('dropMimeData')
        drop_data = data.data('text')
        for item in drop_data:
            if item in self.stringList():
                self.removeRow(self.stringList().index(item))
        return True

    def dragEnterEvent(self, e):
        e.accept()

    def dropEvent(self, event: QDropEvent) -> None:
        event.setDropAction(Qt.MoveAction)
        event.accept()


class Widget(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.widget_layout = QVBoxLayout()
        self.accept_widget = AcceptListWidget()
        self.accept_widget.setFlow(QListWidget.LeftToRight)
        # Create ListWidget and add 10 items to move around.
        self.list_widget = SourceListWidget()
        self.list_widget.setFlow(QListWidget.LeftToRight)
        for x in range(1, 11):
            self.list_widget.addItem('Item {:02d}'.format(x))

        for x in range(1, 2):
            self.accept_widget.addItem('Item {:02d}'.format(x))
        self.widget_layout.addWidget(self.accept_widget)
        self.widget_layout.addWidget(self.list_widget)
        self.setLayout(self.widget_layout)


if __name__ == '__main__':
  app = QApplication(sys.argv)
  widget = Widget()
  widget.show()

  sys.exit(app.exec_())


我要修复的主要方面是AcceptListWidget的内部排序,但是如果可以将项目从SourceListWidget放置到AcceptListWidget的某些位置,那会更好。

最佳答案

默认情况下,QListWidget接受项目的重新排序,因此仅在源为外部时才需要调用父方法:

def dropEvent(self, event: QDropEvent) -> None:
    if event.source() is self:
        super().dropEvent(event)
    else:
        event.setDropAction(Qt.CopyAction)
        self.addItem(event.mimeData().text())

关于python - 如何能够在QListWidgets之间拖放项目,并且仍然能够使用拖放进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55719200/

10-13 00:06