The Wayback Machine - https://web.archive.org/web/20240807171941/https://www.geeksforgeeks.org/how-to-remove-all-items-in-a-qlistwidget-in-pyqt5/
Open In App

How to remove all items in a Qlistwidget in PyQt5?

Last Updated : 25 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Prerequisite:

There are so many options provided by Python to develop GUI applications and PyQt5 is one of them. PyQt5 is a cross-platform GUI toolkit, a set of python bindings for Qt v5. One can develop an interactive desktop application with so much ease because of the tools and simplicity provided by this library.

In this article, we will learn how to remove all items from QlistWidget in PyQt5. To achieve the required functionality i.e. to cleaning the window or deleting all elements using Qlistwidget in Python, its clear() method is used.

Syntax:

clear()

Approach

  • Import module
  • Create QListWidget
  • Add title and button
  • Add mechanism to delete all items of the list when the button is pressed
  • Display window

Program:

Python3




# Import Module
import sys
from PyQt5.QtWidgets import *
  
  
class ListBox(QWidget):
  
    def __init__(self):
        super().__init__()
  
        self.initUI()
  
    def initUI(self):
        # Vertical box layout
        vbox = QVBoxLayout(self)
  
        # Horizontal box layout
        hbox = QHBoxLayout()
  
        # Create QlistWidget Object
        self.listWidget = QListWidget(self)
  
        # Add Items to QlistWidget
        self.listWidget.addItems(
            ['python', 'c++', 'java', 'pyqt5', 'javascript', 'geeksforgeeks'])
  
        # Add Push Button
        clear_btn = QPushButton('Clear', self)
        clear_btn.clicked.connect(self.clearListWidget)
  
        vbox.addWidget(self.listWidget)
        hbox.addWidget(clear_btn)
        vbox.addLayout(hbox)
  
        self.setLayout(vbox)
  
        # Set geometry
        self.setGeometry(300, 300, 350, 250)
  
        # Set window title
        self.setWindowTitle('QListWidget')
  
        # Display QlistWidget
        self.show()
  
    def clearListWidget(self):
        self.listWidget.clear()
  
  
if __name__ == '__main__':
    app = QApplication(sys.argv)
  
    # Call ListBox Class
    ex = ListBox()
  
    # Close the window
    sys.exit(app.exec_())


Output:



Similar Reads

PyQt5 QListWidget - Getting Count of Items
In this article we will see how we can get the count of items of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Count property holds the number of items in the list includin
2 min read
PyQt5 QListWidget – Setting Spacing Between the Items
In this article we will see how we can set the space between the item of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Spacing property holds the space around the items in
2 min read
PyQt5 QListWidget – Getting Spacing Between the Items
In this article we will see how we can get the space between the item of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Spacing property holds the space around the items in
2 min read
PyQt5 QListWidget – Getting all Scroll Bar at given alignment
In this article we will see how we can get the scroll bar of the QListWidget at the given alignment. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Scroll bar can be added to list widget at
2 min read
PyQt5 QListWidget | Python
In PyQt5, QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Syntax: listWidget = QListWidget() There are two ways to add items to the list. They can be constructed with the list widget as their
1 min read
PyQt5 QListWidget - Setting Current Selected Row
In this article we will see how we can set the current row of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Current row property holds the row of the current item. Dependin
2 min read
PyQt5 QListWidget - Getting Current Selected Row
In this article we will see how we can get the current selected row of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Current row property holds the row of the current item.
2 min read
PyQt5 QListWidget - Making Sorting Enabled
In this article we will see how we can set the sorting enabled property of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. This property holds whether sorting is enabled, if
2 min read
PyQt5 QListWidget - Checking if Sorting is Enabled or not
In this article we will see how we can check the sorting enabled property of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. This property holds whether sorting is enabled, i
2 min read
PyQt5 QListWidget - Current Selected Row Signal
In this article we will see how we can get the current selected row changed signal of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. Current row property holds the row of th
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg