Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove Item from QListWidget

Tags:

python

pyqt

I'm stuck using myItem.hide() method every time I need to remove Item from QListWidget list. 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.

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)
like image 832
alphanumeric Avatar asked May 23 '14 18:05

alphanumeric


1 Answers

I don't know why but removeItemWidget don't work as expected. You have to use take item instead:

def removeSel(self):
    listItems=self.listA.selectedItems()
    if not listItems: return        
    for item in listItems:
       self.listA.takeItem(self.listA.row(item))
like image 138
Salvatore Avanzo Avatar answered Oct 26 '22 03:10

Salvatore Avanzo