在 QListWidget 中上下移动项目? [英] Moving items up and down in a QListWidget?

查看:361
本文介绍了在 QListWidget 中上下移动项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

这是我的部分代码:

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

推荐答案

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

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

这篇关于在 QListWidget 中上下移动项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆