简体   繁体   English

如何从QListWidget中删除Item

[英]How to remove Item from QListWidget

I'm stuck using myItem.hide() method every time I need to remove Item from QListWidget list. 每次我需要从QListWidget列表中删除Item时,我都会使用myItem.hide()方法。 Hiding an item instead of deleting/removing makes things unnecessary complex. 隐藏项目而不是删除/删除会使事情变得不必要。 I would appreciate if you show me how to delete Item from ListWidget permanently. 如果您告诉我如何永久地从ListWidget中删除Item,我将不胜感激。

在此输入图像描述


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)

I don't know why but removeItemWidget don't work as expected. 我不知道为什么但removeItemWidget不能按预期工作。 You have to use take item instead: 你必须使用take项目:

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

Posting here an example showing how to implement same approach but now applied to QTreeWidget which a bit more involved than QListWidget . 这里发布一个示例,显示如何实现相同的方法,但现在应用于QTreeWidget ,它比QListWidget更复杂。

在此输入图像描述

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

A ListWidget is a list of ListWidgetItems. ListWidget是ListWidgetItems的列表。 A ListWidgetItems can be assigned a custom widget to override the default, so removeItemWidget() only removes the custom widget. 可以为ListWidgetItems分配一个自定义小部件来覆盖默认小部件,因此removeItemWidget()仅删除自定义小部件。 Hence the need for takeItem, which pops the item from the list and returns it (similar to how a python list works) 因此需要takeItem,它从列表中弹出项目并返回它(类似于python列表的工作方式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM