如何从QListWidget中删除Item

时间:2014-05-23 18:10:19

标签: python pyqt

每次我需要从QListWidget列表中删除Item时,我都会使用myItem.hide()方法。隐藏项目而不是删除/删除会使事情变得不必要。如果您告诉我如何永久地从ListWidget中删除Item,我将不胜感激。

enter image description here


from PyQt4 import QtGui, QtCore

class MyApp(object):    
    def __init__(self):
        super(MyApp, self).__init__()                
        self.mainWidget = QtGui.QWidget()
        self.mainLayout = QtGui.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.hLayout = QtGui.QHBoxLayout()
        self.mainLayout.insertLayout(0, self.hLayout)
        self.listA=QtGui.QListWidget()
        for i in range(3):
            self.listA.addItem('Item '+str(i))
        self.hLayout.addWidget(self.listA)

        self.buttonGroupbox = QtGui.QGroupBox()
        self.buttonlayout = QtGui.QVBoxLayout()
        self.buttonGroupbox.setLayout(self.buttonlayout)

        okButton = QtGui.QPushButton('Remove Selected')
        okButton.clicked.connect(self.removeSel)
        self.buttonlayout.addWidget(okButton)

        self.mainLayout.addWidget(self.buttonGroupbox)
        self.mainWidget.show()
        sys.exit(app.exec_())

    def removeSel(self):
        listItems=self.listA.selectedItems()
        if not listItems: return        
        for item in listItems:
            print type(item), dir(item)

3 个答案:

答案 0 :(得分:9)

我不知道为什么但是removeItemWidget没有按预期工作。你必须使用take item:

def removeSel(self):
    listItems=self.listA.selectedItems()
    if not listItems: return        
    for item in listItems:
       self.listA.takeItem(self.listA.row(item))

答案 1 :(得分:1)

在此处发布一个示例,说明如何实施相同的方法但现在应用于QTreeWidget,其中涉及的内容比QListWidget更多。

enter image description here

from PyQt4 import QtGui, QtCore

class MyApp(object):    
    def __init__(self):
        super(MyApp, self).__init__()                
        self.mainWidget = QtGui.QWidget()
        self.mainLayout = QtGui.QVBoxLayout()
        self.mainWidget.setLayout(self.mainLayout)

        self.hLayout = QtGui.QHBoxLayout()
        self.mainLayout.insertLayout(0, self.hLayout)


        self.listA=QtGui.QTreeWidget()
        self.listA.setColumnCount(3)
        self.listA.setHeaderLabels(['Checkbox','Name','Data'])
        for i in range(3):
            item=QtGui.QTreeWidgetItem()
            item.setCheckState(0,QtCore.Qt.Checked)
            item.setText(1, 'Item '+str(i))
            item.setData(2, QtCore.Qt.UserRole, id(item) )
            item.setText(2, str(id(item) ) )
            self.listA.addTopLevelItem(item)

        self.hLayout.addWidget(self.listA)

        self.buttonGroupbox = QtGui.QGroupBox()
        self.buttonlayout = QtGui.QVBoxLayout()
        self.buttonGroupbox.setLayout(self.buttonlayout)

        okButton = QtGui.QPushButton('Remove Selected')
        okButton.clicked.connect(self.removeSel)
        self.buttonlayout.addWidget(okButton)

        getDataButton = QtGui.QPushButton('Get Items Data')
        getDataButton.clicked.connect(self.getItemsData)
        self.buttonlayout.addWidget(getDataButton)

        self.mainLayout.addWidget(self.buttonGroupbox)
        self.mainWidget.show()
        sys.exit(app.exec_())

    def removeSel(self):
        listItems=self.listA.selectedItems()
        if not listItems: return   
        for item in listItems:
            itemIndex=self.listA.indexOfTopLevelItem(item)
            self.listA.takeTopLevelItem(itemIndex)
        print '\n\t Number of items remaining', self.listA.topLevelItemCount()

    def getItemsData(self):
        for i in range(self.listA.topLevelItemCount()):
            item=self.listA.topLevelItem(i)
            itmData=item.data(2, QtCore.Qt.UserRole)
            itemId=itmData.toPyObject()
            print '\n\t Item Id Stored as Item Data:', itemId, 'Item Checkbox State:', item.checkState(0)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MyApp()

答案 2 :(得分:0)

ListWidget是ListWidgetItems的列表。可以为ListWidgetItems分配一个自定义小部件来覆盖默认小部件,因此removeItemWidget()仅删除自定义小部件。因此需要takeItem,它从列表中弹出项目并返回它(类似于python列表的工作方式)