PyQt - QListWidget



The list widget is the set of items that work on a data parameter value in a simple format. Imagine you are exploring any sites and you are navigating the list from the navbar option.

What is QListWidget?

In PyQt, QListWidget class is an item-based interface to add or remove items from a list. Each item in the list is a QListWidgetItem object. ListWidget can be set to be multiselectable.

Following are the frequently used methods of QListWidget class −

Sr.No. Methods & Description
1

addItem()

Adds QListWidgetItem object or string in the list

2

addItems()

Adds each item in the list

3

insertItem()

Inserts item at the specified index

4

clear()

Removes contents of the list

5

setCurrentItem()

Sets currently selected item programmatically

6

sortItems()

Rearranges items in ascending order

7

setCentralWidget()

centering the widget

8

QListWidgetItem()

add the list of items

Following are the signals emitted by QListWidget −

Sr.No. Methods & Description
1

currentItemChanged()

Whenever current item changes

2

itemClicked()

Whenever an item in the list is clicked

Example 1

In the following example, we listed three items using addItem(), and for selecting individual items has used the signal itemClicked.connect(self.on_item_clicked) that allows the user to select the item from the list.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QListWidget, QListWidgetItem, QVBoxLayout, QWidget
class ListWidgetWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setMinimumSize(500, 250)
      self.setWindowTitle("QListWidget")

      # Create a QListWidget
      self.list_widget = QListWidget(self)

      # Add items to the list
      self.list_widget.addItem("Python")
      self.list_widget.addItem("C#")
      self.list_widget.addItem("Django")

      # Connect item selection signal
      self.list_widget.itemClicked.connect(self.on_item_clicked)

      # Set layout
      layout = QVBoxLayout()
      layout.addWidget(self.list_widget)

      central_widget = QWidget()
      central_widget.setLayout(layout)
      self.setCentralWidget(central_widget)

   def on_item_clicked(self, item):
      print(f"Selected item: {item.text()}")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   main_win = ListWidgetWindow()
   main_win.show()
   sys.exit(app.exec())

Output

The above code produces the following output −

qlistwidget_ex_one

Example 2

Following is the another way to illustrate the code snippet for list widgets in PyQt.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QListWidget, QVBoxLayout, QListWidgetItem
class Ui_MainWindow(QWidget):
   def __init__(self, parent=None):
      super(Ui_MainWindow, self).__init__(parent)

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = QWidget()
   listWidget = QListWidget()
   window.setWindowTitle("QListWidget")

   QListWidgetItem("Welcome", listWidget)
   QListWidgetItem("To", listWidget)
   QListWidgetItem("Hyderabad", listWidget)

   listWidgetItem = QListWidgetItem("Tutorialspoint")
   listWidget.addItem(listWidgetItem)

   window_layout = QVBoxLayout(window)
   window_layout.addWidget(listWidget)
   window.setLayout(window_layout)

   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output−

qlistwidget_ex_two
Advertisements