Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I respond to an internal drag-and-drop operation using a QListWidget?

Tags:

qt

qt4

pyqt

pyqt4

I've got a Qt4 application (using the PyQt bindings) which contains a QListWidget, initialized like so:

class MyList(QtGui.QListWidget):
    def __init__(self):
        QtGui.QListWidget.__init__(self)
        self.setDragDropMode(self.InternalMove)

I can add items, and this allows me to drag and drop to reorder the list. But how do I get notification when the list gets reordered by the user? I tried adding a dropMimeData(self, index, data, action) method to the class, but it never gets called.

like image 660
Chris B. Avatar asked Aug 03 '09 20:08

Chris B.


2 Answers

I know this is old, but I was able to get my code to work using Trey's answer and wanted to share my python solution. This is for a QListWidget inside a QDialog, not one that is sub-classed.

class NotesDialog(QtGui.QDialog):
    def __init__(self, notes_list, notes_dir):
        QtGui.QDialog.__init__(self)
        self.ui=Ui_NotesDialog()
        # the notesList QListWidget is created here (from Qt Designer)
        self.ui.setupUi(self) 

        # install an event filter to catch internal QListWidget drop events
        self.ui.notesList.installEventFilter(self)

    def eventFilter(self, sender, event):
        # this is the function that processes internal drop in notesList
        if event.type() == QtCore.QEvent.ChildRemoved:
            self.update_views() # do something
        return False # don't actually interrupt anything
like image 156
akehrer Avatar answered Sep 28 '22 06:09

akehrer


I have an easier way. :)

You can actually access the listwidget's internal model with myList->model() - and from there there are lots of signals available.

If you only care about drag&drop, connect to layoutChanged. If you have move buttons (which usually are implemented with remove+add) connect to rowsInserted too.

If you want to know what moved, rowsMoved might be better than layoutChanged.

like image 41
Chani Avatar answered Sep 28 '22 07:09

Chani