Moving items up and down in a QListWidget?

这一生的挚爱 提交于 2020-01-15 08:22:07

问题


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

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



来源:https://stackoverflow.com/questions/10957392/moving-items-up-and-down-in-a-qlistwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!