PySide6 QComboBox 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# The QComboBox widget is a combined button and popup list.

import sys

from PySide6.QtCore import Slot, Qt
from PySide6.QtWidgets import (QApplication,
    QWidget, QVBoxLayout, QComboBox, QLabel)


class Window(QWidget):
    
    def __init__(self):

        super().__init__()
        
        layout = QVBoxLayout()
        self.setLayout(layout)
        
        # 1 - Create the combo box
        
        self.combo_box = QComboBox()
        
        # 2 - Add items to it
        
        self.combo_box.addItems(
            ['Linux', 'Windows', 'Mac', 'Android'])
        
        # Set setEditable to False if you
        # want to limit text to your predefined values.
        # False is the default value
        
        self.combo_box.setEditable(True)
        
        self.label_text_changed = QLabel()
        self.label_text_activated = QLabel()
        
        layout.addWidget(self.combo_box)
        layout.addWidget(self.label_text_changed)
        layout.addWidget(self.label_text_activated)
        
        self.combo_box.currentTextChanged.connect(
            self.on_current_text_changed)
            
        self.combo_box.textActivated.connect(
            self.on_text_activated)
    
    # 3 - Handle QComboBox.currentTextChanged signals
    
    @Slot()
    def on_current_text_changed(self, text):
        self.label_text_changed.setText(text)
        self.print_current()

    #     Also handle QComboBox.textActivated
    
    @Slot()
    def on_text_activated(self, text):
        self.label_text_activated.setText(text)
        self.print_current()
        
    def print_current(self):
        print(self.combo_box.currentData(Qt.DisplayRole))
        print(self.combo_box.currentIndex())
        print(self.combo_box.currentText())


if __name__ == '__main__':

    if not QApplication.instance():
        app = QApplication(sys.argv)
    else:
        app = QApplication.instance()

    main_window = Window()
    main_window.show()

    sys.exit(app.exec())

QComboBox combines a button and a pop-up list. When the button is clicked the list pops up and lets you select one of the items which saves precious UI space. To use QComboBox in your application

  1. Create the QComboBox object. There isn’t a constructor that accepts a list of combobox items.

  2. Add items to the combobox. You can manipulate individual QComboBox items using addItem(), insertItem() and removeItem(). You can manipulate items en gros using addItems(), insertItems() and clear(). Lastly, as QComboBox is part of the Qt model-view framework, you can set its items using its model() property.

  3. Write the methods to handle QComboBox signals. QComboBox emits several types of signals and the ones of interest are currentIndexChanged, currentTextChanged and textActivated. In your slots, you can use currentData(), currentText() and currentIndex() to get information about the selected item which we demonstrate in the example.