Moving items up and down in a QListWidget?

Solution 1:

Well, after trying in different ways, this is solved by taking the selected entry and inserting it into a new position.

For the Up Button is something like this:

    currentRow = self.ventana.listWidget.currentRow()
    currentItem = self.ventana.listWidget.takeItem(currentRow)
    self.ventana.listWidget.insertItem(currentRow - 1, currentItem)

And for the Down Button it's the same, except that in the third line the "-" sign is changed by a "+".

Solution 2:

A Better Way

I know you got your answer. But here's a little advice or you can say advancement to your Project.

listwidget.setDragDropMode(QAbstractItemView.InternalMove)
listwidget.model().rowsMoved.connect(lambda: anyfunction())
  1. The Line 1 of this code will allow you to drag the item to any location and change it in seconds.
  2. The Line 2 of this code will allow to trigger a function after you finish with drag and drop.