6

In a QListWidget I have a set of entries. Now I want to allow the user to sort (reorder) these entries through two buttons (Up/Down).

Here's part of my code:

def __init__(self):
    QtGui.QMainWindow.__init__(self)

    self.ventana = Ui_MainWindow()
    self.ventana.setupUi(self)

    self.connect(self.ventana.btExit, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))

    self.connect(self.ventana.btAdd, QtCore.SIGNAL('clicked()'), self.addButton)
    self.connect(self.ventana.btQuit, QtCore.SIGNAL('clicked()'), self.quitButton)
    self.connect(self.ventana.btQuitAll, QtCore.SIGNAL('clicked()'), self.quitAllButton)
    self.connect(self.ventana.btUp, QtCore.SIGNAL('clicked()'), self.upButton)
    self.connect(self.ventana.btDown, QtCore.SIGNAL('clicked()'), self.downButton)

def addButton(self):
    fileNames = QtGui.QFileDialog.getOpenFileNames(self, 'Agregar archivos')
    self.ventana.listWidget.addItems(fileNames)

def quitButton(self):
    item = self.ventana.listWidget.takeItem(self.ventana.listWidget.currentRow())
    item = None

def quitAllButton(self):
    self.ventana.listWidget.clear()

def upButton(self):
   # HOW TO MOVE ITEM
Maw Ceron
  • 319
  • 3
  • 9
  • Duplicate of one I answer in the past? http://stackoverflow.com/a/9166163/496445 – jdi Jun 09 '12 at 00:41
  • Actually I solved this in a simpler way, but thank you – Maw Ceron Jun 09 '12 at 02:11
  • maybe post your answer and accept it then to close this down. Or at least update your question to show you resolved it. – jdi Jun 09 '12 at 03:47
  • Yea thats a simplified version of what my answer describes, though mine is meant to support multiple row selection and maintain the original selection – jdi Jun 09 '12 at 05:31
  • Please post your solution as an answer, you will be able to accept it in 2 days. In that time, the question may receive other answers or your answer and question may be improved. Your question and answer may also receive up votes. Until a question has an accepted answer, it will continue to show up in SO as unanswered. – Arnold Spence Jun 09 '12 at 05:52

2 Answers2

14

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 "+".

Maw Ceron
  • 319
  • 3
  • 9
  • Good solution, and will work very nicely for text based list items. If your items become widgets, it gets a lot more complicated. [This answer](https://stackoverflow.com/a/57915796/9705687) shows some of the details in for widget list items. – bfris Sep 13 '19 at 22:34
4

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.
Hamza
  • 132
  • 5