Python PySide6
About Lesson

In this lesson we want to learn about Python PySide6 QComboBox, QComboBox widget is  drop down list that allows users to select from set of predefined items. it is very useful widget for building user interfaces, as it allows for the selection of single item from a list of options. QComboBox widget is part of the Qt library, which is the foundation of PySide6 framework.

 

 

Creating a QComboBox

Creating QComboBox in PySide6 is simple. this is an example of how to create QComboBox:

In this example we have created new PySide6 application and QMainWindow object. after that we have created QComboBox object and set it as member variable of QMainWindow object. Finally, we set the window geometry and title, and show the window.

 

 

Run the complete code and this will be the result.

Python PySide6 QComboBox
Python PySide6 QComboBox

 

 

Adding Items to the QComboBox

Once we have created QComboBox, we can add items to it. this is an example of how to add items to QComboBox:

In this example we have added three items to the QComboBox using the addItem() method. each item is string that will be displayed in the drop down list.

 

 

Run the complete code and this will be the result.

Python PySide6 QComboBox
Python PySide6 QComboBox

 

 

Retrieving the Selected Item

For retrieving selected item from the QComboBox, we can use currentText() method. this is an example of how to retrieve selected item:

 

 

Now let’s add signals and slots functionality to our app, this is an example that demonstrates the use of signals and slots with QComboBox in PySide6:

In this example we have created new PySide6 application and QMainWindow object. After that we have created QComboBox object and added three items to it using addItem() method. also we have connected currentIndexChanged signal of QComboBox to on_combo_changed slot using the connect() method. finally we have defined on_combo_changed slot, which is called when the selected item in the QComboBox is changed. the currentText() method is used to retrieve currently selected item which is then printed to console.

@Slot(int) decorator is used to indicate that on_combo_changed method is slot that takes an integer argument. this is because currentIndexChanged signal of QComboBox passes the index of the selected item as an integer argument to the slot.