python - 在 QListWidget 中上下移动项目?

标签 python qt4 pyqt4 qlistwidget

在 QListWidget 中,我有一组条目。现在我想让用户通过两个按钮(向上/向下)对这些条目进行排序(重新排序)。

这是我的部分代码:

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

最佳答案

好吧,在尝试了不同的方法之后,通过获取所选条目并将其插入到新位置来解决这个问题。

对于向上按钮是这样的:

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

对于向下按钮,它是相同的,除了在第三行中的“-”符号被“+”更改。

关于python - 在 QListWidget 中上下移动项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10957392/

相关文章:

使用 Qt4 进行 Python 分散更新 - Qslider

qt - 如何将 QAbstractTableModel 和 QItemDelegate 组合到一个工作源?

c++ - 如何防止 qmake 在此 "moc"构建方案中创建额外的 'out of source' 文件夹?

python - django 测试中的修补(模拟)表单

python - 无法写入文件但未生成错误

linux - 在 Linux 中通过 QProcess 执行 gzip

python - PyQT 中心工具栏按钮

Python PyQt 根据用户在旋转框中选择的数量创建组合框

python - 如何在事件中获取小部件名称?

python - 字典理解从具有重复键值的元组列表中获取每个键控项目的平均值