QComboBox Class
The QComboBox class is a button that drops down a list of items to choose from. More…
| Header | #include <QComboBox> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QWidget |
| Inherited by | QFontComboBox |
List of all members, including inherited members
Public Functions
| QComboBox(QWidget *parent = nullptr) | |
| ~QComboBox() | |
| int | count() const |
| QString | itemText(int index) const |
| void | setItemText(int index, const QString &text) |
| int | currentIndex() const |
| QString | currentText() const |
| int | findText(const QString &text) const |
| int | maxVisibleItems() const |
| void | setMaxVisibleItems(int count) |
| bool | isPopupVisible() const |
| void | addItem(const QString &text) |
| void | addItems(const QStringList &texts) |
| void | insertItem(int index, const QString &text) |
| void | removeItem(int index) |
Reimplemented Public Functions
| QSize | sizeHint() const |
| QSize | minimumSizeHint() const |
Public Slots
| void | setCurrentIndex(int index) |
| void | setCurrentText(const QString &text) |
| void | clear() |
| void | showPopup() |
| void | hidePopup() |
Signals
| void | currentIndexChanged(int index) |
| void | currentTextChanged(const QString &text) |
| void | activated(int index) |
| void | highlighted(int index) |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | mousePressEvent(QMouseEvent *event) |
| void | mouseReleaseEvent(QMouseEvent *event) |
| void | wheelEvent(QWheelEvent *event) |
| void | keyPressEvent(QKeyEvent *event) |
| void | changeEvent(QEvent *event) |
Detailed Description
A combo box shows the current item. Clicking it opens a list in which the user chooses another one, and the arrow keys and the mouse wheel change it without opening the list. Connect to currentIndexChanged() or activated():
QComboBox *font = new QComboBox(window);
font->addItems({"Small", "Medium", "Large"});
connect(font, &QComboBox::currentIndexChanged, [](int index) { qDebug() << index; });
The list is a widget inside the window of the combo box, not a window of its own, so it never reaches outside the window. It opens below the combo box, or above it if there is more room there.
Combo boxes: closed, disabled, and open, with the list below it.

See also QSpinBox and QRadioButton.
Differences from Qt
The items are plain texts: there are no icons, no item data (setItemData(), currentData()), no model or delegate, and no QComboBox::InsertPolicy. A combo box cannot be edited (setEditable()), and has no setPlaceholderText(), setMinimumContentsLength() or setSizeAdjustPolicy(). Typing does not jump to an item.
Member Function Documentation
QComboBox::QComboBox(QWidget *parent = nullptr)
Constructs an empty combo box with the given parent.
QComboBox::~QComboBox()
Destroys the combo box.
void QComboBox::activated(int index)
This signal is emitted when the user chooses the item at index, also when it is the current one already.
void QComboBox::addItem(const QString &text)
Adds an item with text at the end.
See also insertItem().
void QComboBox::addItems(const QStringList &texts)
Adds an item for each of texts at the end.
See also addItem().
void QComboBox::changeEvent(QEvent *event)
Reimplements QWidget::changeEvent().
void QComboBox::clear()
Removes all items.
See also removeItem().
int QComboBox::count() const
Returns the number of items.
int QComboBox::currentIndex() const
Returns the index of the current item, or -1 if there is none. The first item added becomes the current one.
See also setCurrentIndex().
void QComboBox::currentIndexChanged(int index)
This signal is emitted when the current item changes to the one at index, which is -1 if there is none. Its cause can be the user, setCurrentIndex() or a change of the items.
QString QComboBox::currentText() const
Returns the text of the current item, or an empty string if there is none.
See also setCurrentText().
void QComboBox::currentTextChanged(const QString &text)
This signal is emitted when the text of the current item changes to text.
int QComboBox::findText(const QString &text) const
Returns the index of the first item whose text is text, or -1 if there is none.
void QComboBox::hidePopup()
Closes the list of items, if it is open.
See also showPopup().
void QComboBox::highlighted(int index)
This signal is emitted when the user moves over the item at index in the popup.
void QComboBox::insertItem(int index, const QString &text)
Inserts an item with text at index, which is brought into the range of the items. The current item stays the same, except that the first item that is added becomes the current one.
See also addItem() and removeItem().
bool QComboBox::isPopupVisible() const
Returns whether the list of items is open.
See also showPopup().
QString QComboBox::itemText(int index) const
Returns the text of the item at index, or an empty string if there is no such item.
See also setItemText().
void QComboBox::keyPressEvent(QKeyEvent *event)
Reimplements QWidget::keyPressEvent().
int QComboBox::maxVisibleItems() const
Returns how many items the list shows at most, 10 by default. A longer list scrolls.
See also setMaxVisibleItems().
QSize QComboBox::minimumSizeHint() const
Reimplements QWidget::minimumSizeHint().
void QComboBox::mousePressEvent(QMouseEvent *event)
Reimplements QWidget::mousePressEvent().
void QComboBox::mouseReleaseEvent(QMouseEvent *event)
Reimplements QWidget::mouseReleaseEvent().
void QComboBox::paintEvent(QPaintEvent *event)
Reimplements QWidget::paintEvent().
void QComboBox::removeItem(int index)
Removes the item at index. If it was the current one, the item that takes its place becomes current.
See also clear() and insertItem().
void QComboBox::setCurrentIndex(int index)
Makes the item at index the current one. An index outside the items makes it -1, which shows nothing. Emits currentIndexChanged() and currentTextChanged() if the item changes.
See also currentIndex().
void QComboBox::setCurrentText(const QString &text)
Makes the first item with text the current one. Nothing happens if there is none.
See also currentText() and findText().
void QComboBox::setItemText(int index, const QString &text)
Sets the text of the item at index to text. Nothing happens if there is no such item.
See also itemText().
void QComboBox::setMaxVisibleItems(int count)
Sets the number of items that the list shows at most to count.
See also maxVisibleItems().
void QComboBox::showPopup()
Opens the list of items.
See also hidePopup().
QSize QComboBox::sizeHint() const
Reimplements QWidget::sizeHint().
void QComboBox::wheelEvent(QWheelEvent *event)
Reimplements QWidget::wheelEvent().