PyQt:如何在ListView的onDrop()之后获取listItem索引

2024-09-29 05:29:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这个例子创建了一个简单的对话框窗口,其中有两个支持拖放的listWidgets(感谢Ekhurvo!)。 在dropEvent上,droppedOnA()和droppedOnB()函数将输出一个信息:这些项来自哪些listWidgets以及它们曾经拥有哪些索引。但是我需要找到在将删除的listItems放到QListWidget接收器后分配的索引。如果能解释一下如何做到这一点,我将不胜感激。在

from PyQt4 import QtGui, QtCore
import sys, os

class MyClass(object):
        def __init__(self):
            super(MyClass, self).__init__()               

class ThumbListWidget(QtGui.QListWidget):
    _drag_info = []
    def __init__(self, type, name, parent=None):
        super(ThumbListWidget, self).__init__(parent)
        self.setObjectName(name)
        self.setIconSize(QtCore.QSize(124, 124))
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.setAcceptDrops(True)

    def startDrag(self, actions):
        self._drag_info[:] = [str(self.objectName())]
        for item in self.selectedItems():
            self._drag_info.append(self.row(item))
        super(ThumbListWidget, self).startDrag(actions)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            super(ThumbListWidget, self).dragEnterEvent(event)

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            super(ThumbListWidget, self).dragMoveEvent(event)

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            # event.setDropAction(QtCore.Qt.MoveAction)
            super(ThumbListWidget, self).dropEvent(event)
            self.emit(QtCore.SIGNAL("dropped"), self._drag_info )


class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()
        self.listItems={}

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidgetA = ThumbListWidget(self, 'MainTree')
        self.listWidgetB = ThumbListWidget(self, 'SecondaryTree')

        for i in range(7):
            listItemA=QtGui.QListWidgetItem()
            listItemA.setText('A'+'%04d'%i)
            self.listWidgetA.addItem(listItemA)

            myClassInstA=MyClass()
            listItemA.setData(QtCore.Qt.UserRole, myClassInstA)

            listItemB=QtGui.QListWidgetItem()
            listItemB.setText('A'+'%04d'%i)
            self.listWidgetB.addItem(listItemB)

            myClassInstB=MyClass()
            listItemB.setData(QtCore.Qt.UserRole, myClassInstB)

        myBoxLayout.addWidget(self.listWidgetA)      

        myBoxLayout.addWidget(self.listWidgetB)   
        self.connect(self.listWidgetA, QtCore.SIGNAL("dropped"), self.droppedOnA)
        self.connect(self.listWidgetB, QtCore.SIGNAL("dropped"), self.droppedOnB)


    def droppedOnA(self, dropped_list):
        print '\n\t dropped On MainTree', dropped_list, 

    def droppedOnB(self, dropped_list):
        print '\n\t dropped On SecondaryTree', dropped_list


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(720,480)
    sys.exit(app.exec_())

Tags: selfinfoeventifinitdefmyclassqt
1条回答
网友
1楼 · 发布于 2024-09-29 05:29:12

未进行销毁测试,但请尝试重新实现rowsInserted并从那里发出draginfo:

class ThumbListWidget(QtGui.QListWidget):
    _drag_info = []
    def __init__(self, type, name, parent=None):
        ...
        self._dropping = False

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            # event.setDropAction(QtCore.Qt.MoveAction)
            self._dropping = True
            super(ThumbListWidget, self).dropEvent(event)
            self._dropping = False

    def rowsInserted(self, parent, start, end):
        if self._dropping:
            self._drag_info.append((start, end))
            self.emit(QtCore.SIGNAL("dropped"), self._drag_info)
        super(ThumbListWidget, self).rowsInserted(parent, start, end)

相关问题 更多 >