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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from PySide6.QtWidgets import QApplication, QComboBox, QMainWindow import sys class ComboExample(QMainWindow): def __init__(self): super().__init__() # create a QComboBox self.combo = QComboBox(self) # set the window geometry and title, and show the window self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Combo Box Example') self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = ComboExample() sys.exit(app.exec()) |
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.
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:
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 |
from PySide6.QtWidgets import QApplication, QComboBox, QMainWindow import sys class ComboExample(QMainWindow): def __init__(self): super().__init__() # create a QComboBox self.combo = QComboBox(self) # add items to the QComboBox self.combo.addItem("Python") self.combo.addItem("Java") self.combo.addItem("C++") # set the window geometry and title, and show the window self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Combo Box Example') self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = ComboExample() sys.exit(app.exec()) |
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.
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:
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 |
from PySide6.QtWidgets import QApplication, QComboBox, QMainWindow import sys class ComboExample(QMainWindow): def __init__(self): super().__init__() # create QComboBox self.combo = QComboBox(self) # add items to the QComboBox self.combo.addItem("Python") self.combo.addItem("Java") self.combo.addItem("C++") # set the window geometry and title, and show the window self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Combo Box Example') self.show() # print the selected item to the console print(self.combo.currentText()) if __name__ == '__main__': app = QApplication(sys.argv) window = ComboExample() sys.exit(app.exec()) |
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:
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 |
from PySide6.QtWidgets import QApplication, QComboBox, QMainWindow from PySide6.QtCore import Slot import sys class Example(QMainWindow): def __init__(self): super().__init__() # create QComboBox self.combo = QComboBox(self) # add items to the QComboBox self.combo.addItem("Python") self.combo.addItem("Java") self.combo.addItem("C++") # connect the currentIndexChanged signal to the on_combo_changed slot self.combo.currentIndexChanged.connect(self.on_combo_changed) # set the window geometry and title, and show the window self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Combo Box Example') self.show() @Slot(int) def on_combo_changed(self, index): print(self.combo.currentText()) 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 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.