Search code examples
python-2.7pyqtpyqt4

PYQT4 List item contents are disappearing when inserting it back into the list


I have a pyqt4 setup with a custom widget as the item of the listwidget. I have two buttons in that custom widget to move it up or down the list by taking it and inserting it either 1 up or 1 down.

When it is inserted the item is still highlighted but the contents are gone.

Here is what is moving the item.

def ChangeInit(self, row, direction):
        item = self.initiativeList.takeItem(row)
        self.initiativeList.insertItem(row + direction, item)

row = the row the item is in
direction = 1 or -1 depending on which button is pressed

Any ideas why the item appears to be moved but the contents of it not being moved with it or at least not visible?

Let me know if you need more info.


Solution

  • The documentation of QListWidget.addItem states:

    Warning: A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior.

    Although this can be interpreted in more than one way (i.e. adding one widget multiple times simultaneously, or adding it sequentially like you do) I sugest that you create a completely new QListWidgetItem object and insert that in the list, just to be sure. Otherwise I don't know if Qt will handle the underlying indices correctly.

    P.S. next time I would add the general PyQt label unless it is a problem specific to PyQt4. This might get you some more views (you now only have two after 23 hours).