QComboBox
A QComboBox
is a button that provides a list of options to the user when clicked.
The user can select an option from the list. The selected option is displayed
in the combo box.
Signals
currentIndexChanged
: Emitted when the current index changes.
Methods
Examples
combo = QtWidgets.QComboBox()
combo.addItem("Option 1")
combo.addItems(["Option 2", "Option 3"])
print(combo.currentText())
'Option 1'
'0'
'Option 3'
''
'-1'
addItem(text)
Adds an item to the combo box with the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The text to display in the combo box. |
required |
addItems(texts)
Adds the items in the list texts to the combo box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts |
Sequence[str]
|
The list of strings to add to the combo box. |
required |
clear()
Clears the contents of the combo box, removing all items.
currentIndex()
Gets the index of the currently selected item in the combo box. For an empty combo box, returns -1. The current index can change when inserting or removing items.
Returns:
Type | Description |
---|---|
int
|
The index of the currently selected item. |
currentText()
Gets the text of the currently selected item in the combo box. For an empty combo box, returns an empty string.
Returns:
Type | Description |
---|---|
str
|
The text of the currently selected item. |
removeItem(index)
Removes the item at the given index from the combo box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
The index of the item to remove. |
required |
setCurrentIndex(index)
Sets the current index of the combo box to the given index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
The index of the item to set as the current item. |
required |
setCurrentText(text)
Sets the current text of the combo box to the given text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The text to set as the current text. |
required |