QListWidget中如何移动项目上下?

2024-09-24 02:26:55 发布

您现在位置:Python中文网/ 问答频道 /正文

在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

Tags: selfsignal排序initdefconnect条目qtgui
1条回答
网友
1楼 · 发布于 2024-09-24 02:26:55

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

因为向上按钮是这样的:

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

对于Down按钮也是一样的,只是在第三行“-”符号被“+”改变了。在

相关问题 更多 >