Search code examples
pythonqlistwidget

How to remove ListWidget Item from ListWidget


There are 5 items in a ListWidget. Pressing button prints out current Item and its Index. It seems that should be sufficient information to remove/delete the item from the ListWidget.

The question: How to remove a currently selected list Item from a ListWidget?

from PyQt4 import QtGui, QtCore
import sys, os

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidgetA = QtGui.QListWidget()
        for i in range(5):
            QtGui.QListWidgetItem( 'Item '+str(i), self.listWidgetA )

        self.listWidgetA.itemClicked.connect(self.item_clicked)

        myBoxLayout.addWidget(self.listWidgetA)

        myButton=QtGui.QPushButton("Delete")
        myBoxLayout.addWidget(myButton)
        myButton.clicked.connect(self.deleteItem) 

    def item_clicked(self, arg):
        print '\n\t selectedItem',  arg
        print '\n\t indexFromItem',  self.listWidgetA.indexFromItem(arg)

    def deleteItem(self):
        print '\n\t\t currentItem:', self.listWidgetA.currentItem()
        print '\n\t\t currentIndex:', self.listWidgetA.currentIndex()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

Solution

  • Here is the working code showing two ways to delete the List Item. Thanks everyone!

    from PyQt4 import QtGui, QtCore
    import sys, os
    
    class Dialog_01(QtGui.QMainWindow):
        def __init__(self):
            super(QtGui.QMainWindow,self).__init__()
    
            myQWidget = QtGui.QWidget()
            myBoxLayout = QtGui.QVBoxLayout()
            myQWidget.setLayout(myBoxLayout)
            self.setCentralWidget(myQWidget)
    
            self.listWidgetA = QtGui.QListWidget()
            for i in range(5):
                QtGui.QListWidgetItem( 'Item '+str(i), self.listWidgetA )
    
            self.listWidgetA.itemClicked.connect(self.item_clicked)
    
            myBoxLayout.addWidget(self.listWidgetA)
    
            myButtonA=QtGui.QPushButton("Delete Item Using Approach A")
            myBoxLayout.addWidget(myButtonA)
            myButtonA.clicked.connect(self.deleteItemA) 
    
            myButtonB=QtGui.QPushButton("Delete Item Using Approach B")
            myBoxLayout.addWidget(myButtonB)
            myButtonB.clicked.connect(self.deleteItemB) 
    
    
    
        def item_clicked(self, arg):
            print '\n\t selectedItem',  arg
            print '\n\t indexFromItem',  self.listWidgetA.indexFromItem(arg)
    
    
        def deleteItemA(self):
            item = self.listWidgetA.takeItem(self.listWidgetA.currentRow())
            item = None
    
    
        def deleteItemB(self):
            model = self.listWidgetA.model()
            for selectedItem in self.listWidgetA.selectedItems():
                qIndex = self.listWidgetA.indexFromItem(selectedItem)
                print 'removing : %s' %model.data(qIndex).toString()
                model.removeRow(qIndex.row())
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        dialog_1 = Dialog_01()
        dialog_1.show()
        dialog_1.resize(480,320)
        sys.exit(app.exec_())