|  | Home · All Classes · Modules

# QComboBox Class Reference
[QtGui module]

The QComboBox widget is a combined button and popup list. More...

Inherits QWidget.

Inherited by QFontComboBox.

### Types

- enum InsertPolicy { NoInsert, InsertAtTop, InsertAtCurrent, InsertAtBottom, ..., InsertAlphabetically }
- enum SizeAdjustPolicy { AdjustToContents, AdjustToContentsOnFirstShow, AdjustToMinimumContentsLength, AdjustToMinimumContentsLengthWithIcon }

### Methods

- __init__ (self, QWidget parent = None)
- addItem (self, QString, QVariant userData = QVariant())
- addItem (self, QIcon, QString, QVariant userData = QVariant())
- addItems (self, QStringList)
- bool autoCompletion (self)
- Qt.CaseSensitivity autoCompletionCaseSensitivity (self)
- changeEvent (self, QEvent)
- clear (self)
- clearEditText (self)
- QCompleter completer (self)
- contextMenuEvent (self, QContextMenuEvent)
- int count (self)
- int currentIndex (self)
- QString currentText (self)
- bool duplicatesEnabled (self)
- bool event (self, QEvent)
- int findData (self, QVariant, int role = Qt.UserRole, Qt.MatchFlags flags = Qt.MatchExactly|Qt.MatchCaseSensitive)
- int findText (self, QString, Qt.MatchFlags flags = Qt.MatchExactly|Qt.MatchCaseSensitive)
- focusInEvent (self, QFocusEvent)
- focusOutEvent (self, QFocusEvent)
- bool hasFrame (self)
- hideEvent (self, QHideEvent)
- hidePopup (self)
- QSize iconSize (self)
- initStyleOption (self, QStyleOptionComboBox)
- inputMethodEvent (self, QInputMethodEvent)
- QVariant inputMethodQuery (self, Qt.InputMethodQuery)
- insertItem (self, int, QString, QVariant userData = QVariant())
- insertItem (self, int, QIcon, QString, QVariant userData = QVariant())
- insertItems (self, int, QStringList)
- InsertPolicy insertPolicy (self)
- insertSeparator (self, int)
- bool isEditable (self)
- QVariant itemData (self, int, int role = Qt.UserRole)
- QAbstractItemDelegate itemDelegate (self)
- QIcon itemIcon (self, int)
- QString itemText (self, int)
- keyPressEvent (self, QKeyEvent)
- keyReleaseEvent (self, QKeyEvent)
- QLineEdit lineEdit (self)
- int maxCount (self)
- int maxVisibleItems (self)
- int minimumContentsLength (self)
- QSize minimumSizeHint (self)
- QAbstractItemModel model (self)
- int modelColumn (self)
- mousePressEvent (self, QMouseEvent)
- mouseReleaseEvent (self, QMouseEvent)
- paintEvent (self, QPaintEvent)
- removeItem (self, int)
- resizeEvent (self, QResizeEvent)
- QModelIndex rootModelIndex (self)
- setAutoCompletion (self, bool)
- setAutoCompletionCaseSensitivity (self, Qt.CaseSensitivity)
- setCompleter (self, QCompleter)
- setCurrentIndex (self, int)
- setDuplicatesEnabled (self, bool)
- setEditable (self, bool)
- setEditText (self, QString)
- setFrame (self, bool)
- setIconSize (self, QSize)
- setInsertPolicy (self, InsertPolicy)
- setItemData (self, int, QVariant, int role = Qt.UserRole)
- setItemDelegate (self, QAbstractItemDelegate)
- setItemIcon (self, int, QIcon)
- setItemText (self, int, QString)
- setLineEdit (self, QLineEdit)
- setMaxCount (self, int)
- setMaxVisibleItems (self, int)
- setMinimumContentsLength (self, int)
- setModel (self, QAbstractItemModel)
- setModelColumn (self, int)
- setRootModelIndex (self, QModelIndex)
- setSizeAdjustPolicy (self, SizeAdjustPolicy)
- setValidator (self, QValidator)
- setView (self, QAbstractItemView)
- showEvent (self, QShowEvent)
- showPopup (self)
- SizeAdjustPolicy sizeAdjustPolicy (self)
- QSize sizeHint (self)
- QValidator validator (self)
- QAbstractItemView view (self)
- wheelEvent (self, QWheelEvent)

### Qt Signals

- void activated (int)
- void activated (const QString&)
- void currentIndexChanged (int)
- void currentIndexChanged (const QString&)
- void editTextChanged (const QString&)
- void highlighted (int)
- void highlighted (const QString&)

## Detailed Description

The QComboBox widget is a combined button and popup list.

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 two signals emitted if the current item of a combobox changes, currentIndexChanged() and activated(). currentIndexChanged() is 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 selectes 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 is AtBottom 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.

See also QLineEdit, QSpinBox, QRadioButton, QButtonGroup, and GUI Design Handbook: Combo Box, Drop-Down List Box.

## Type Documentation

### 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.

### 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.AdjustToMinimumContentsLength` | `2` | Use AdjustToContents or AdjustToContentsOnFirstShow instead.
`QComboBox.AdjustToMinimumContentsLengthWithIcon` | `3` | The combobox will adjust to minimumContentsLength plus space for an icon. For performance reasons use this policy on large models.

## Method Documentation

### QComboBox.__init__ (self, QWidget parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a combobox with the given parent, using the default model QStandardItemModel.

### QComboBox.addItem (self, QString, QVariant userData = QVariant())

Adds an item to the combobox with the given text, and containing the specified userData (stored in the Qt.UserRole). The item is appended to the list of existing items.

### QComboBox.addItem (self, QIcon, QString, QVariant userData = QVariant())

Adds an item to the combobox with the given icon and text, and containing the specified userData (stored in the Qt.UserRole). The item is appended to the list of existing items.

### QComboBox.addItems (self, QStringList)

Adds each of the strings in the given texts to the combobox. Each item is appended to the list of existing items in turn.

### bool QComboBox.autoCompletion (self)

### Qt.CaseSensitivity QComboBox.autoCompletionCaseSensitivity (self)

### QComboBox.changeEvent (self, QEvent)

Reimplemented from QWidget.changeEvent().

### QComboBox.clear (self)

This method is also a Qt slot with the C++ signature `void clear()`.

Clears the combobox, removing all items.

Note: If you have set an external model on the combobox this model will still be cleared when calling this function.

### QComboBox.clearEditText (self)

This method is also a Qt slot with the C++ signature `void clearEditText()`.

Clears the contents of the line edit used for editing in the combobox.

### QCompleter QComboBox.completer (self)

Returns the completer that is used to auto complete text input for the combobox.

This function was introduced in Qt 4.2.

See also setCompleter() and editable.

### QComboBox.contextMenuEvent (self, QContextMenuEvent)

Reimplemented from QWidget.contextMenuEvent().

### int QComboBox.count (self)

### int QComboBox.currentIndex (self)

### QString QComboBox.currentText (self)

### bool QComboBox.duplicatesEnabled (self)

### bool QComboBox.event (self, QEvent)

Reimplemented from QObject.event().

### int QComboBox.findData (self, QVariant, int role = Qt.UserRole, Qt.MatchFlags flags = Qt.MatchExactly|Qt.MatchCaseSensitive)

Returns the index of the item containing the given data for the given role; otherwise returns -1.

The flags specify how the items in the combobox are searched.

### int QComboBox.findText (self, QString, Qt.MatchFlags flags = Qt.MatchExactly|Qt.MatchCaseSensitive)

Returns the index of the item containing the given text; otherwise returns -1.

The flags specify how the items in the combobox are searched.

### QComboBox.focusInEvent (self, QFocusEvent)

Reimplemented from QWidget.focusInEvent().

### QComboBox.focusOutEvent (self, QFocusEvent)

Reimplemented from QWidget.focusOutEvent().

### bool QComboBox.hasFrame (self)

### QComboBox.hideEvent (self, QHideEvent)

Reimplemented from QWidget.hideEvent().

### QComboBox.hidePopup (self)

Hides the list of items in the combobox if it is currently visible and resets the internal state, so that if the custom pop-up was shown inside the reimplemented showPopup(), then you also need to reimplement the hidePopup() function to hide your custom pop-up and call the base class implementation to reset the internal state whenever your custom pop-up widget is hidden.

See also showPopup().

### QSize QComboBox.iconSize (self)

### QComboBox.initStyleOption (self, QStyleOptionComboBox)

Initialize option with the values from this QComboBox. This method is useful for subclasses when they need a QStyleOptionComboBox, but don't want to fill in all the information themselves.

See also QStyleOption.initFrom().

### QComboBox.inputMethodEvent (self, QInputMethodEvent)

Reimplemented from QWidget.inputMethodEvent().

### QVariant QComboBox.inputMethodQuery (self, Qt.InputMethodQuery)

Reimplemented from QWidget.inputMethodQuery().

### QComboBox.insertItem (self, int, QString, QVariant userData = QVariant())

Inserts the text and userData (stored in the Qt.UserRole) into the combobox at the given index.

If the index is equal to or higher than the total number of items, the new item is appended to the list of existing items. If the index is zero or negative, the new item is prepended to the list of existing items.

See also insertItems().

### QComboBox.insertItem (self, int, QIcon, QString, QVariant userData = QVariant())

Inserts the icon, text and userData (stored in the Qt.UserRole) into the combobox at the given index.

If the index is equal to or higher than the total number of items, the new item is appended to the list of existing items. If the index is zero or negative, the new item is prepended to the list of existing items.

See also insertItems().

### QComboBox.insertItems (self, int, QStringList)

Inserts the strings from the list into the combobox as separate items, starting at the index specified.

If the index is equal to or higher than the total number of items, the new items are appended to the list of existing items. If the index is zero or negative, the new items are prepended to the list of existing items.

See also insertItem().

### InsertPolicy QComboBox.insertPolicy (self)

### QComboBox.insertSeparator (self, int)

Inserts a separator item into the combobox at the given index.

If the index is equal to or higher than the total number of items, the new item is appended to the list of existing items. If the index is zero or negative, the new item is prepended to the list of existing items.

This function was introduced in Qt 4.4.

See also insertItem().

### bool QComboBox.isEditable (self)

### QVariant QComboBox.itemData (self, int, int role = Qt.UserRole)

Returns the data for the given role in the given index in the combobox, or QVariant.Invalid if there is no data for this role.

See also setItemData().

### QAbstractItemDelegate QComboBox.itemDelegate (self)

Returns the item delegate used by the popup list view.

See also setItemDelegate().

### QIcon QComboBox.itemIcon (self, int)

Returns the icon for the given index in the combobox.

See also setItemIcon().

### QString QComboBox.itemText (self, int)

Returns the text for the given index in the combobox.

See also setItemText().

### QComboBox.keyPressEvent (self, QKeyEvent)

Reimplemented from QWidget.keyPressEvent().

### QComboBox.keyReleaseEvent (self, QKeyEvent)

Reimplemented from QWidget.keyReleaseEvent().

### QLineEdit QComboBox.lineEdit (self)

Returns the line edit used to edit items in the combobox, or 0 if there is no line edit.

Only editable combo boxes have a line edit.

See also setLineEdit().

### int QComboBox.maxCount (self)

### int QComboBox.maxVisibleItems (self)

### int QComboBox.minimumContentsLength (self)

### QSize QComboBox.minimumSizeHint (self)

Reimplemented from QWidget.minimumSizeHint().

### QAbstractItemModel QComboBox.model (self)

Returns the model used by the combobox.

See also setModel().

### int QComboBox.modelColumn (self)

### QComboBox.mousePressEvent (self, QMouseEvent)

Reimplemented from QWidget.mousePressEvent().

### QComboBox.mouseReleaseEvent (self, QMouseEvent)

Reimplemented from QWidget.mouseReleaseEvent().

### QComboBox.paintEvent (self, QPaintEvent)

Reimplemented from QWidget.paintEvent().

### QComboBox.removeItem (self, int)

Removes the item at the given index from the combobox. This will update the current index if the index is removed.

This function does nothing if index is out of range.

### QComboBox.resizeEvent (self, QResizeEvent)

Reimplemented from QWidget.resizeEvent().

### QModelIndex QComboBox.rootModelIndex (self)

Returns the root model item index for the items in the combobox.

See also setRootModelIndex().

### QComboBox.setAutoCompletion (self, bool)

### QComboBox.setAutoCompletionCaseSensitivity (self, Qt.CaseSensitivity)

### QComboBox.setCompleter (self, QCompleter)

Sets the completer to use instead of the current completer. If completer is 0, auto completion is disabled.

By default, for an editable combo box, a QCompleter that performs case insensitive inline completion is automatically created.

This function was introduced in Qt 4.2.

See also completer().

### QComboBox.setCurrentIndex (self, int)

### QComboBox.setDuplicatesEnabled (self, bool)

### QComboBox.setEditable (self, bool)

### QComboBox.setEditText (self, QString)

This method is also a Qt slot with the C++ signature `void setEditText(const QString&)`.

Sets the text in the combobox's text edit.

### QComboBox.setFrame (self, bool)

### QComboBox.setIconSize (self, QSize)

### QComboBox.setInsertPolicy (self, InsertPolicy)

### QComboBox.setItemData (self, int, QVariant, int role = Qt.UserRole)

Sets the data role for the item on the given index in the combobox to the specified value.

See also itemData().

### QComboBox.setItemDelegate (self, QAbstractItemDelegate)

Sets the item delegate for the popup list view. The combobox takes ownership of the delegate.

Warning: You should not share the same instance of a delegate between comboboxes, widget mappers or views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() signal, and attempt to access, modify or close an editor that has already been closed.

See also itemDelegate().

### QComboBox.setItemIcon (self, int, QIcon)

Sets the icon for the item on the given index in the combobox.

See also itemIcon().

### QComboBox.setItemText (self, int, QString)

Sets the text for the item on the given index in the combobox.

See also itemText().

### QComboBox.setLineEdit (self, QLineEdit)

The QLineEdit argument has it's ownership transferred to Qt.

Sets the line edit to use instead of the current line edit widget.

The combo box takes ownership of the line edit.

See also lineEdit().

### QComboBox.setMaxCount (self, int)

### QComboBox.setMaxVisibleItems (self, int)

### QComboBox.setMinimumContentsLength (self, int)

### QComboBox.setModel (self, QAbstractItemModel)

Sets the model to be model. model must not be 0. If you want to clear the contents of a model, call clear().

See also model() and clear().

### QComboBox.setModelColumn (self, int)

### QComboBox.setRootModelIndex (self, QModelIndex)

Sets the root model item index for the items in the combobox.

See also rootModelIndex().

### QComboBox.setSizeAdjustPolicy (self, SizeAdjustPolicy)

### QComboBox.setValidator (self, QValidator)

Sets the validator to use instead of the current validator.

See also validator().

### QComboBox.setView (self, QAbstractItemView)

The QAbstractItemView argument has it's ownership transferred to Qt.

Sets the view to be used in the combobox popup to the given itemView. The combobox takes ownership of the view.

Note: If you want to use the convenience views (like QListWidget, QTableWidget or QTreeWidget), make sure to call setModel() on the combobox with the convenience widgets model before calling this function.

See also view().

### QComboBox.showEvent (self, QShowEvent)

Reimplemented from QWidget.showEvent().

### QComboBox.showPopup (self)

Displays the list of items in the combobox. If the list is empty then the no items will be shown.

If you reimplement this function to show a custom pop-up, make sure you call hidePopup() to reset the internal state.

See also hidePopup().

### SizeAdjustPolicy QComboBox.sizeAdjustPolicy (self)

### QSize QComboBox.sizeHint (self)

Reimplemented from QWidget.sizeHint().

This implementation caches the size hint to avoid resizing when the contents change dynamically. To invalidate the cached value change the sizeAdjustPolicy.

### QValidator QComboBox.validator (self)

Returns the validator that is used to constrain text input for the combobox.

See also setValidator() and editable.

### QAbstractItemView QComboBox.view (self)

Returns the list view used for the combobox popup.

See also setView().

### QComboBox.wheelEvent (self, QWheelEvent)

Reimplemented from QWidget.wheelEvent().

## Qt Signal Documentation

### void activated (int)

This is the default overload of this signal.

This signal is sent when the user chooses an item in the combobox. The item's index is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().

### void activated (const QString&)

This signal is sent when the user chooses an item in the combobox. The item's text is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().

### void currentIndexChanged (int)

This is the default overload of this signal.

This signal is sent whenever the currentIndex in the combobox changes either through user interaction or programmatically. The item's index is passed or -1 if the combobox becomes empty or the currentIndex was reset.

This function was introduced in Qt 4.1.

### void currentIndexChanged (const QString&)

This signal is sent whenever the currentIndex in the combobox changes either through user interaction or programmatically. The item's text is passed.

This function was introduced in Qt 4.1.

### void editTextChanged (const QString&)

This is the default overload of this signal.

This signal is emitted when the text in the combobox's line edit widget is changed. The new text is specified by text.

### void highlighted (int)

This is the default overload of this signal.

This signal is sent when an item in the combobox popup list is highlighted by the user. The item's index is passed.

### void highlighted (const QString&)

This signal is sent when an item in the combobox popup list is highlighted by the user. The item's text is passed.