In this lesson we want to learn about Python PySide6 QListWidget, one of the most useful widgets provided by PySide6 is QListWidget, which is used to display list of items in user interface.
Getting Started with QListWidget
For using PySide6 QListWidget in your application, you will first need to import it from the QtWidgets module. you can then create an instance of the QListWidget and add items to it using addItem() method. this is simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
from PySide6.QtWidgets import QApplication, QVBoxLayout,QListWidget, QListWidgetItem, QMainWindow import sys class Example(QMainWindow): def __init__(self): super().__init__() # set the window geometry and title, and show the window self.setGeometry(300, 300, 300, 200) self.setWindowTitle('List Widget Example') vbox = QVBoxLayout() # create a QListWidget self.list_widget = QListWidget(self) # add items to the list item1 = QListWidgetItem("Python") item2 = QListWidgetItem("Java") item3 = QListWidgetItem("C++") self.list_widget.addItem(item1) self.list_widget.addItem(item2) self.list_widget.addItem(item3) vbox.addWidget(self.list_widget) self.setLayout(vbox) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) |
In this example we have created new PySide6 application and QMainWindow object. after that we have created QListWidget object and add three QListWidgetItem objects to it using the addItem() method. Finally, we set the window geometry and title and show the window.
Run the code and this will be the result.
Customizing QListWidget
PySide6 QListWidget is highly customizable and provides different options for controlling appearance and behavior of the list. these are a few of the most common customization options:
Sorting the List
You can sort the items in the list using the sortItems() method. By default, the sorting is done in ascending order. this is an example:
1 |
self.list_widget.sortItems() |
Setting the Current Item
You can set the current item in the list using setCurrentItem() method. this is an example:
1 |
self.list_widget.setCurrentItem(item2) |
Selecting Items
You can select one or more items in list using the setItemSelected() method. this is an example that selects the first and third items in the list:
1 2 |
self.list_widget.setItemSelected(item1, True) self.list_widget.setItemSelected(item3, True) |
Removing Items
You can remove items from the list using the takeItem() method. this is an example that removes the second item from the list:
1 |
self.list_widget.takeItem(1) |
Signals and Slots
Like other PySide6 widgets QListWidget provides different signals and slots that you can use to add interactivity to your application. for example you can connect the itemClicked() signal to slot that handles the event when an item in the list is clicked. this is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from PySide6.QtWidgets import QApplication, QListWidget, QListWidgetItem, QMainWindow import sys class Example(QMainWindow): def __init__(self): super().__init__() # create a QListWidget self.list_widget = QListWidget(self) # add items to the list item1 = QListWidgetItem("Item 1") item2 = QListWidgetItem("Item 2") item3 = QListWidgetItem("Item 3") self.list_widget.addItem(item1) self.list_widget.addItem(item2) self.list_widget.addItem(item3) # set the window geometry and title, and show the window self.setGeometry(300, 300, 300, 200) self.setWindowTitle('List Widget Example') # connect the itemClicked signal to a custom slot self.list_widget.itemClicked.connect(self.on_item_clicked) # create a layout and add the list widget to it layout = self.layout() layout.addWidget(self.list_widget) self.show() # define a custom slot to handle itemClicked signal def on_item_clicked(self, item): print(f"{item.text()} clicked.") if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) |
In this example have we create new PySide6 application and QMainWindow object. after that we have created QListWidget object and added three QListWidgetItem objects to it using addItem() method. also we have connected the itemClicked signal of the QListWidget to custom slot called on_item_clicked.
on_item_clicked slot is defined to take an argument that represents the item that was clicked in the list. In this case we simply print the text of the clicked item.
Finally we create layout and add the list widget to it using the addWidget() method. we set window geometry and title and show the window. when an item in the list is clicked, the on_item_clicked slot is called, and the text of the clicked item is printed to the console.