QComboBox Class
The QComboBox widget is a combined button and popup list. More...
| Header: | #include <QComboBox> |
| qmake: | QT += widgets |
| Inherits: | QWidget |
| Inherited By: |
Public Types
| enum | InsertPolicy { NoInsert, InsertAtTop, InsertAtCurrent, InsertAtBottom, InsertAfterCurrent, …, InsertAlphabetically } |
| enum | SizeAdjustPolicy { AdjustToContents, AdjustToContentsOnFirstShow, AdjustToMinimumContentsLengthWithIcon } |
Properties
|
|
Public Functions
| int | count() const |
| QVariant | currentData(int role = Qt::UserRole) const |
| int | currentIndex() const |
| QString | currentText() const |
| bool | duplicatesEnabled() const |
| bool | hasFrame() const |
| QSize | iconSize() const |
| QComboBox::InsertPolicy | insertPolicy() const |
| bool | isEditable() const |
| int | maxCount() const |
| int | maxVisibleItems() const |
| int | minimumContentsLength() const |
| int | modelColumn() const |
| QString | placeholderText() const |
| void | setDuplicatesEnabled(bool enable) |
| void | setEditable(bool editable) |
| void | setFrame(bool) |
| void | setIconSize(const QSize &size) |
| void | setInsertPolicy(QComboBox::InsertPolicy policy) |
| void | setMaxCount(int max) |
| void | setMaxVisibleItems(int maxItems) |
| void | setMinimumContentsLength(int characters) |
| void | setModelColumn(int visibleColumn) |
| void | setPlaceholderText(const QString &placeholderText) |
| void | setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy policy) |
| QComboBox::SizeAdjustPolicy | sizeAdjustPolicy() const |
Public Slots
| void | setCurrentIndex(int index) |
| void | setCurrentText(const QString &text) |
Signals
| void | currentIndexChanged(const QString &) |
| void | currentIndexChanged(int index) |
| void | currentTextChanged(const QString &) |
Detailed Description

A QComboBox provides a means of presenting a list of options to the user in a way that takes up the minimum amount of screen space.
A combobox is a selection widget that displays the current item, and can pop up a list of selectable items. A combobox may be editable, allowing the user to modify each item in the list.
Comboboxes can contain pixmaps as well as strings; the insertItem() and setItemText() functions are suitably overloaded. For editable comboboxes, the function clearEditText() is provided, to clear the displayed string without changing the combobox's contents.
There are three signals emitted if the current item of a combobox changes, currentIndexChanged(), currentTextChanged() and activated(). currentIndexChanged() and currentTextChanged() are always emitted regardless if the change was done programmatically or by user interaction, while activated() is only emitted when the change is caused by user interaction. The highlighted() signal is emitted when the user highlights an item in the combobox popup list. All three signals exist in two versions, one with a QString argument and one with an int argument. If the user selects or highlights a pixmap, only the int signals are emitted. Whenever the text of an editable combobox is changed the editTextChanged() signal is emitted.
When the user enters a new string in an editable combobox, the widget may or may not insert it, and it can insert it in several locations. The default policy is InsertAtBottom but you can change this using setInsertPolicy().
It is possible to constrain the input to an editable combobox using QValidator; see setValidator(). By default, any input is accepted.
A combobox can be populated using the insert functions, insertItem() and insertItems() for example. Items can be changed with setItemText(). An item can be removed with removeItem() and all items can be removed with clear(). The text of the current item is returned by currentText(), and the text of a numbered item is returned with text(). The current item can be set with setCurrentIndex(). The number of items in the combobox is returned by count(); the maximum number of items can be set with setMaxCount(). You can allow editing using setEditable(). For editable comboboxes you can set auto-completion using setCompleter() and whether or not the user can add duplicates is set with setDuplicatesEnabled().
QComboBox uses the model/view framework for its popup list and to store its items. By default a QStandardItemModel stores the items and a QListView subclass displays the popuplist. You can access the model and view directly (with model() and view()), but QComboBox also provides functions to set and get item data (e.g., setItemData() and itemText()). You can also set a new model and view (with setModel() and setView()). For the text and icon in the combobox label, the data in the model that has the Qt::DisplayRole and Qt::DecorationRole is used. Note that you cannot alter the SelectionMode of the view(), e.g., by using setSelectionMode().
See also QLineEdit, QSpinBox, QRadioButton, QButtonGroup, and GUI Design Handbook: Combo Box, Drop-Down List Box.
Member Type Documentation
enum QComboBox::InsertPolicy
This enum specifies what the QComboBox should do when a new string is entered by the user.
| Constant | Value | Description |
|---|---|---|
QComboBox::NoInsert | 0 | The string will not be inserted into the combobox. |
QComboBox::InsertAtTop | 1 | The string will be inserted as the first item in the combobox. |
QComboBox::InsertAtCurrent | 2 | The current item will be replaced by the string. |
QComboBox::InsertAtBottom | 3 | The string will be inserted after the last item in the combobox. |
QComboBox::InsertAfterCurrent | 4 | The string is inserted after the current item in the combobox. |
QComboBox::InsertBeforeCurrent | 5 | The string is inserted before the current item in the combobox. |
QComboBox::InsertAlphabetically | 6 | The string is inserted in the alphabetic order in the combobox. |
enum QComboBox::SizeAdjustPolicy
This enum specifies how the size hint of the QComboBox should adjust when new content is added or content changes.
| Constant | Value | Description |
|---|---|---|
QComboBox::AdjustToContents | 0 | The combobox will always adjust to the contents |
QComboBox::AdjustToContentsOnFirstShow | 1 | The combobox will adjust to its contents the first time it is shown. |
QComboBox::AdjustToMinimumContentsLengthWithIcon | AdjustToContentsOnFirstShow + 2 | The combobox will adjust to minimumContentsLength plus space for an icon. For performance reasons use this policy on large models. |
Property Documentation
count : const int
This property holds the number of items in the combobox
By default, for an empty combo box, this property has a value of 0.
Access functions:
| int | count() const |
currentData : const QVariant
This property holds the data for the current item
By default, for an empty combo box or a combo box in which no current item is set, this property contains an invalid QVariant.
This property was introduced in Qt 5.2.
Access functions:
| QVariant | currentData(int role = Qt::UserRole) const |
currentIndex : int
This property holds the index of the current item in the combobox.
The current index can change when inserting or removing items.
By default, for an empty combo box or a combo box in which no current item is set, this property has a value of -1.
Access functions:
| int | currentIndex() const |
| void | setCurrentIndex(int index) |
Notifier signal:
| void | currentIndexChanged(int index) |
| void | currentIndexChanged(const QString &) |
currentText : QString
This property holds the current text
If the combo box is editable, the current text is the value displayed by the line edit. Otherwise, it is the value of the current item or an empty string if the combo box is empty or no current item is set.
The setter setCurrentText() simply calls setEditText() if the combo box is editable. Otherwise, if there is a matching text in the list, currentIndex is set to the corresponding index.
Access functions:
| QString | currentText() const |
| void | setCurrentText(const QString &text) |
Notifier signal:
| void | currentTextChanged(const QString &) |
See also editable and setEditText().
duplicatesEnabled : bool
This property holds whether the user can enter duplicate items into the combobox
Note that it is always possible to programmatically insert duplicate items into the combobox.
By default, this property is false (duplicates are not allowed).
Access functions:
| bool | duplicatesEnabled() const |
| void | setDuplicatesEnabled(bool enable) |
editable : bool
This property holds whether the combo box can be edited by the user
By default, this property is false. The effect of editing depends on the insert policy.
Note: When disabling the editable state, the validator and completer are removed.
Access functions:
| bool | isEditable() const |
| void | setEditable(bool editable) |
See also InsertPolicy.
frame : bool
This property holds whether the combo box draws itself with a frame
If enabled (the default) the combo box draws itself inside a frame, otherwise the combo box draws itself without any frame.
Access functions:
| bool | hasFrame() const |
| void | setFrame(bool) |
iconSize : QSize
This property holds the size of the icons shown in the combobox.
Unless explicitly set this returns the default value of the current style. This size is the maximum size that icons can have; icons of smaller size are not scaled up.
Access functions:
| QSize | iconSize() const |
| void | setIconSize(const QSize &size) |
insertPolicy : InsertPolicy
This property holds the policy used to determine where user-inserted items should appear in the combobox
The default value is InsertAtBottom, indicating that new items will appear at the bottom of the list of items.
Access functions:
| QComboBox::InsertPolicy | insertPolicy() const |
| void | setInsertPolicy(QComboBox::InsertPolicy policy) |
See also InsertPolicy.
maxCount : int
This property holds the maximum number of items allowed in the combobox
Note: If you set the maximum number to be less then the current amount of items in the combobox, the extra items will be truncated. This also applies if you have set an external model on the combobox.
By default, this property's value is derived from the highest signed integer available (typically 2147483647).
Access functions:
| int | maxCount() const |
| void | setMaxCount(int max) |
maxVisibleItems : int
This property holds the maximum allowed size on screen of the combo box, measured in items
By default, this property has a value of 10.
Note: This property is ignored for non-editable comboboxes in styles that returns true for QStyle::SH_ComboBox_Popup such as the Mac style or the Gtk+ Style.
Access functions:
| int | maxVisibleItems() const |
| void | setMaxVisibleItems(int maxItems) |
minimumContentsLength : int
This property holds the minimum number of characters that should fit into the combobox.
The default value is 0.
If this property is set to a positive value, the minimumSizeHint() and sizeHint() take it into account.
Access functions:
| int | minimumContentsLength() const |
| void | setMinimumContentsLength(int characters) |
See also sizeAdjustPolicy.
modelColumn : int
This property holds the column in the model that is visible.
If set prior to populating the combo box, the pop-up view will not be affected and will show the first column (using this property's default value).
By default, this property has a value of 0.
Access functions:
| int | modelColumn() const |
| void | setModelColumn(int visibleColumn) |
placeholderText : QString
Sets a placeholderText text shown when no valid index is set
The placeholderText will be shown when an invalid index is set. The text is not accessible in the dropdown list. When this function is called before items are added the placeholder text will be shown, otherwise you have to call setCurrentIndex(-1) programmatically if you want to show the placeholder text. Set an empty placeholder text to reset the setting.
When the QComboBox is editable, use QLineEdit::setPlaceholderText() instead.
This property was introduced in Qt 5.15.
Access functions:
| QString | placeholderText() const |
| void | setPlaceholderText(const QString &placeholderText) |
sizeAdjustPolicy : SizeAdjustPolicy
This property holds the policy describing how the size of the combobox changes when the content changes
The default value is AdjustToContentsOnFirstShow.
Access functions:
| QComboBox::SizeAdjustPolicy | sizeAdjustPolicy() const |
| void | setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy policy) |
See also SizeAdjustPolicy.