Search code examples
qteventspyqtdrag-and-dropqlistwidget

Qt After drop event


I need to process some data AFTER a drop event has occured on a widget.

i.e a user selects some items in list A and drops them in list B. My program needs to compare the 2 lists after B has added the items selected from A.

Any ideas?


Solution

  • Here's a PyQt script that demonstrates two ways to trap drop events:

    from PyQt4 import QtGui, QtCore
    
    class Window(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            widget = QtGui.QWidget(self)
            self.setCentralWidget(widget)
            layout = QtGui.QVBoxLayout(widget)
            self.listA = ListWidget(self)
            self.listB = QtGui.QListWidget(self)
            self.listB.viewport().installEventFilter(self)
            for widget in (self.listA, self.listB):
                widget.setAcceptDrops(True)
                widget.setDragEnabled(True)
                for item in 'One Two Three Four Five Six'.split():
                    widget.addItem(item)
                layout.addWidget(widget)
    
        def eventFilter(self, source, event):
            if (event.type() == QtCore.QEvent.Drop and
                source is self.listB.viewport()):
                self.listB.dropEvent(event)
                if event.isAccepted():
                    print 'eventFilter', self.listB.count()
                return True
            return QtGui.QMainWindow.eventFilter(self, source, event)
    
    class ListWidget(QtGui.QListWidget):
        def __init__(self, parent):
            QtGui.QListWidget.__init__(self, parent)
    
        def dropEvent(self, event):
            QtGui.QListWidget.dropEvent(self, event)
            if event.isAccepted():
                print 'dropEvent', self.count()
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())