| Classes - Annotated - Tree - Functions - Home - Structure

# QComboBox Class Reference

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

`#include <qcombobox.h>`

Inherits QWidget.

List of all member functions.

## Public Members

- QComboBox ( QWidget * parent=0, const char * name=0 )
- QComboBox ( bool rw, QWidget * parent=0, const char * name=0 )
- ~QComboBox ()
- int count () const
- void insertStringList ( const QStringList &, int index=-1 )
- void insertStrList ( const QStrList &, int index=-1 )
- void insertStrList ( const QStrList *, int index=-1 )
- void insertStrList ( const char **, int numStrings=-1, int index=-1 )
- void insertItem ( const QString & text, int index=-1 )
- void insertItem ( const QPixmap & pixmap, int index=-1 )
- void insertItem ( const QPixmap & pixmap, const QString & text, int index=-1 )
- void removeItem ( int index )
- void clear ()
- QString currentText () const
- QString text ( int index ) const
- const QPixmap* pixmap ( int index ) const
- void changeItem ( const QString & text, int index )
- void changeItem ( const QPixmap & pixmap, int index )
- void changeItem ( const QPixmap & pixmap, const QString & text, int index )
- int currentItem () const
- virtual void setCurrentItem ( int index )
- bool autoResize () const (obsolete)
- virtual void setAutoResize ( bool ) (obsolete)
- virtual void setBackgroundColor ( const QColor & )
- virtual void setPalette ( const QPalette & )
- virtual void setFont ( const QFont & )
- virtual void setSizeLimit ( int )
- int sizeLimit () const
- virtual void setMaxCount ( int )
- int maxCount () const
- enum Policy { NoInsertion, AtTop, AtCurrent, AtBottom, AfterCurrent, BeforeCurrent }
- virtual void setInsertionPolicy ( Policy policy )
- Policy insertionPolicy () const
- virtual void setValidator ( const QValidator * )
- const QValidator* validator () const
- virtual void setListBox ( QListBox * )
- QListBox* listBox () const
- QLineEdit* lineEdit () const
- virtual void setAutoCompletion ( bool )
- bool autoCompletion () const
- void setDuplicatesEnabled ( bool enable )
- bool duplicatesEnabled () const
- bool editable () const
- void setEditable ( bool )

## Public Slots

- void clearValidator ()
- void clearEdit ()
- virtual void setEditText ( const QString & )

## Signals

- void activated ( int index )
- void highlighted ( int index )
- void activated ( const QString & )
- void highlighted ( const QString & )
- void textChanged ( const QString & )

## Protected Members

- void popup ()

## Properties

Type | Name | READ | WRITE | Options
--- | --- | --- | --- | ---
bool | editable | editable | setEditable |
int | count | count |  |
QString | currentText | currentText |  |
int | currentItem | currentItem | setCurrentItem |
bool | autoResize | autoResize | setAutoResize |
int | sizeLimit | sizeLimit | setSizeLimit |
int | maxCount | maxCount | setMaxCount |
Policy | insertionPolicy | insertionPolicy | setInsertionPolicy |
bool | autoCompletion | autoCompletion | setAutoCompletion |
bool | duplicatesEnabled | duplicatesEnabled | setDuplicatesEnabled |

## Detailed Description

The QComboBox widget is a combined button and popup list.

A combo box may be defined as a selection widget which displays the current selection, and which can pop up a list of possible selections. Some combo boxes also allow the user to select arbitrary strings, using a line editor.

Since combo boxes occupy little screen space and always display the current selection, they are very well suited to displaying and selecting modes (such as font family and size): The user can always see what mode he/she is in, and the majority of the screen space is available for real work.

QComboBox supports three different appearances: Motif 1.x, Motif 2.0 and Windows 95. In Motif 1.x, a combo box was called XmOptionMenu. In Motif 2.0, OSF introduced an improved combo box and named that XmComboBox. QComboBox provides both.

QComboBox provides two different constructors. The simplest one creates an old-style combo box in Motif style:

          QComboBox * c = new QComboBox( this, "read-only combo" );

The other one creates a new-style combo box in Motif style, and can create both read-only and read-write combo boxes:

          QComboBox * c1 = new QComboBox( FALSE, this, "read-only combo" );
          QComboBox * c2 = new QComboBox( TRUE, this, "read-write combo" );

New-style combo boxes use a list box in both Motif and Windows styles, and both the content size and the on-screen size of the list box can be limited. Old-style combo boxes use a popup in Motif style, and that popup will happily grow larger than the desktop if you put enough data in it.

The two constructors create identical-looking combos in Windows style.

Combo boxes can contain pixmaps as well as texts; the insert() and changeItem() functions are suitably overloaded. For read-write combo boxes, the function clearEdit() is provided, to clear the displayed string without changing the combo box' contents.

A combo box emits two signals, activated() and highlighted(), when a new item has been activated (selected) or highlighted (set to current). Both signals exist in two versions, one with a `char*` argument and one with an `int` argument. If the user highlights or activates a pixmap, only the `int` signals are emitted.

When the user enters a new string in a read-write combo, the widget may or may not insert it, and it can insert it in several locations. The default policy is is `AtBottom,` you can change it using setInsertionPolicy().

It is possible to constrain the input to an editable combo box using QValidator; see setValidator(). By default, all input is accepted.

If the combo box is not editable then it has a default focusPolicy() of `TabFocus,` i.e. it will not grab focus if clicked. This differs from both Windows and Motif. If the combo box is editable then it has a default focusPolicy() of `StrongFocus,` i.e. it will grab focus if clicked.

(Motif 1, read-only)
(Motif 2, read-write)
(Motif 2, read-only)
(Windows style)

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

Examples: qmag/qmag.cpp

## Member Type Documentation

### QComboBox::Policy

This enum type specifies what QComboBox should do with a new string entered by the user. The following policies are defined:

- `NoInsertion` means not to insert the string in the combo.
- `AtTop` means to insert the string at the top of the combo box.
- `AtCurrent` means to replace the previously selected item with the typed string.
- `AtBottom` means to insert the string at the bottom of the combo box.
- `AfterCurrent` means to to insert the string just after the previously selected item.
- `BeforeCurrent` means to to insert the string just before the previously selected item.

activated() is always emitted, of course.

If inserting the new string would cause the combo box to breach its content size limit, the item at the other end of the list is deleted. The definition of "other end" is implementation-dependent.

## Member Function Documentation

### QComboBox::QComboBox ( QWidget * parent=0, const char * name=0 )

Constructs a combo box widget with a parent and a name.

This constructor creates a popup menu if the program uses Motif look and feel; this is compatible with Motif 1.x.

### QComboBox::QComboBox ( bool rw, QWidget * parent=0, const char * name=0 )

Constructs a combo box with a maximum size and either Motif 2.0 or Windows look and feel.

The input field can be edited if rw is TRUE, otherwise the user may only choose one of the items in the combo box.

### QComboBox::~QComboBox ()

Destructs the combo box.

### void QComboBox::activated ( const QString & string ) `[signal]`

This signal is emitted when a new item has been activated (selected). string is the activated string.

You can also use activated(int) signal, but be aware that its argument is meaningful only for selected strings, not for typed strings.

### void QComboBox::activated ( int index ) `[signal]`

This signal is emitted when a new item has been activated (selected). The index is the position of the item in the popup list.

### bool QComboBox::autoCompletion () const

Returns TRUE if this combo box is in auto-completion mode.

See also setAutoCompletion().

### bool QComboBox::autoResize () const

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

Returns TRUE if auto-resizing is enabled, or FALSE if auto-resizing is disabled.

Auto-resizing is disabled by default.

See also setAutoResize().

### void QComboBox::changeItem ( const QPixmap & im, const QString & t, int index )

Replaces the item at position index with a pixmap plus text.

See also insertItem().

### void QComboBox::changeItem ( const QPixmap & im, int index )

Replaces the item at position index with a pixmap, unless the combo box is writable.

See also insertItem().

### void QComboBox::changeItem ( const QString & t, int index )

Replaces the item at position index with a text.

### void QComboBox::clear ()

Removes all combo box items.

### void QComboBox::clearEdit () `[slot]`

Clears the line edit without changing the combo's contents. Does nothing if the combo isn't editable.

This is particularly handy when using a combo box as a line edit with history. For example you can connect the combo's activated() signal to clearEdit() in order to present the user with a new, empty line as soon as return is pressed.

See also setEditText().

### void QComboBox::clearValidator () `[slot]`

This slot is equivalent to setValidator( 0 ).

### int QComboBox::count () const

Returns the number of items in the combo box.

### int QComboBox::currentItem () const

Returns the index of the current combo box item.

See also setCurrentItem().

### QString QComboBox::currentText () const

Returns the text item being edited, or the current text item if the combo box is not editable.

See also text().

### bool QComboBox::duplicatesEnabled () const

Returns TRUE if the same text can be inserted multiple times into the list of the combobox, else FALSE.

See also setDuplicatesEnabled();.

### bool QComboBox::editable () const

Returns whether the combobox is editable or not.

See also setEditable().

### bool QComboBox::eventFilter ( QObject * object, QEvent * event ) `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QObject.

### void QComboBox::focusInEvent ( QFocusEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QComboBox::highlighted ( const QString & string ) `[signal]`

This signal is emitted when a new item has been highlighted. string is the highlighted string.

You can also use highlighted(int) signal.

### void QComboBox::highlighted ( int index ) `[signal]`

This signal is emitted when a new item has been set to current. The index is the position of the item in the popup list.

### void QComboBox::insertItem ( const QPixmap & pixmap, const QString & text, int index=-1 )

Inserts a pixmap item with additional text text at position index. The item will be appended if index is negative.

### void QComboBox::insertItem ( const QPixmap & pixmap, int index=-1 )

Inserts a pixmap item at position index. The item will be appended if index is negative.

### void QComboBox::insertItem ( const QString & t, int index=-1 )

Inserts a text item at position index. The item will be appended if index is negative.

### void QComboBox::insertStrList ( const QStrList * list, int index=-1 )

Inserts the list of strings at the index index in the combo box.

This is only for compatibility, as it does not support Unicode strings. See insertStringList().

### void QComboBox::insertStrList ( const char ** strings, int numStrings=-1, int index=-1 )

Inserts the array of ASCII strings at the index index in the combo box.

The numStrings argument is the number of strings. If numStrings is -1 (default), the strs array must be terminated with 0.

Example:

        static const char* items[] = { "red", "green", "blue", 0 };
        combo->insertStrList( items );

Examples: qmag/qmag.cpp

### void QComboBox::insertStrList ( const QStrList & list, int index=-1 )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

### void QComboBox::insertStringList ( const QStringList & list, int index=-1 )

Inserts the list of strings at the index index in the combo box.

### QComboBox::Policy QComboBox::insertionPolicy() const

Returns the current insertion policy of the combo box.

See also setInsertionPolicy().

### void QComboBox::keyPressEvent ( QKeyEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### QLineEdit* QComboBox::lineEdit () const

Returns the line editor, or 0 if there is no line editor currently.

Only editable listboxes have a line editor.

### QListBox * QComboBox::listBox () const

Returns the current list box, or 0 if there is no list box currently. (QComboBox can use QPopupMenu instead of QListBox.) Provided to match setListBox().

See also setListBox().

### int QComboBox::maxCount () const

Returns the current maximum size of the combo box. By default, there is no limit, so this function returns INT_MAX.

See also setMaxCount() and count().

### void QComboBox::mouseDoubleClickEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QComboBox::mouseMoveEvent ( QMouseEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QComboBox::mousePressEvent ( QMouseEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QComboBox::mouseReleaseEvent ( QMouseEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QComboBox::paintEvent ( QPaintEvent * ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### const QPixmap * QComboBox::pixmap ( int index ) const

Returns the pixmap item at a given index, or 0 if the item is not a pixmap.

### void QComboBox::popup () `[protected]`

Popups the combo box popup list.

If the list is empty, no selections appear.

### void QComboBox::removeItem ( int index )

Removes the item at position index.

### void QComboBox::resizeEvent ( QResizeEvent * e ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### void QComboBox::setAutoCompletion ( bool enable ) `[virtual]`

Sets this combo box to offer auto-completion while the user is editing if enable is TRUE, or not to offer auto-completion of enable is FALSE (the default).

The combo box uses the list of items as candidates for completion.

Note: This will only work on editable combo boxes, so make the combo box editable before you call this function or it will not work.

See also autoCompletion() and setEditText().

### void QComboBox::setAutoResize ( bool enable ) `[virtual]`

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

Enables auto-resizing if enable is TRUE, or disables it if enable is FALSE.

When auto-resizing is enabled, the combo box button will resize itself whenever the current combo box item change.

See also autoResize() and adjustSize().

### void QComboBox::setBackgroundColor ( const QColor & color ) `[virtual]`

Reimplements QWidget::setBackgroundColor().

Sets the background color for both the combo box button and the combo box popup list.

### void QComboBox::setCurrentItem ( int index ) `[virtual]`

Sets the current combo box item. This is the item to be displayed on the combo box button.

See also currentItem().

Examples: qmag/qmag.cpp

### void QComboBox::setDuplicatesEnabled ( bool enable )

If the combobox is editable and the user enters some text in the lineedit of the combobox and presses return (and the insertionPolicy() is different from `NoInsertion),` the entered text is inserted into the list of this combobox. Now, if you set enable to TRUE here, this new text is always inserted, else it's only inserted if it doesn't already exist in the list. If you set enable to FALSE and the text exists already in the list, the item which contains the same text like which should be inserted, this item gets the new current item.

This setting only applies when the user want's to insert a text with pressing the return key. It does not affect methods like insertItem() and similar.

### void QComboBox::setEditText ( const QString & newText ) `[virtual slot]`

Sets the text in the embedded line edit to newText without changing the combo's contents. Does nothing if the combo isn't editable.

This is useful e.g. for providing a good starting point for the user's editing and entering the change in the combo only when the user presses enter.

See also clearEdit() and insertItem().

### void QComboBox::setEditable ( bool y )

Make the input field editable, if y is TRUE. Otherwise the user may only choose one of the items in the combo box.

See also editable().

### void QComboBox::setEnabled ( bool enable ) `[virtual]`

Reimplemented for internal reasons; the API is not affected.

### void QComboBox::setFont ( const QFont & font ) `[virtual]`

Reimplements QWidget::setFont().

Sets the font for both the combo box button and the combo box popup list.

### void QComboBox::setInsertionPolicy ( Policy policy )

Sets the insertion policy of the combo box to policy.

The insertion policy governs where items typed in by the user are inserted in the list. The possible values are

- `NoInsertion:` Strings typed by the user aren't inserted anywhere
- `AtTop:` Strings typed by the user are inserted above the top item in the list
- AtCurrent: Strings typed by the user replace the last selected item
- AtBottom: Strings typed by the user are inserted at the bottom of the list.

The default insertion policy is `AtBottom.`

See also insertionPolicy().

### void QComboBox::setListBox ( QListBox * newListBox ) `[virtual]`

Sets the combo box to use newListBox instead of the current list box or popup. As a side effect, clears the combo box of its current contents.

Warning: QComboBox assumes that newListBox->text(n) returns non-null for 0 <= n < newListbox->count(). This assumption is necessary because of the line edit in QComboBox.

### void QComboBox::setMaxCount ( int count ) `[virtual]`

Sets the maximum number of items the combo box can hold to count.

If count is smaller than the current number of items, the list is truncated at the end. There is no limit by default.

See also maxCount() and count().

### void QComboBox::setPalette ( const QPalette & palette ) `[virtual]`

Reimplements QWidget::setPalette().

Sets the palette for both the combo box button and the combo box popup list.

### void QComboBox::setSizeLimit ( int lines ) `[virtual]`

Sets the maximum on-screen size of the combo box to lines. This is disregarded in Motif 1.x style. The default limit is ten lines.

If the number of items in the combo box is/grows larger than `lines,` a list box is added.

See also sizeLimit(), count() and setMaxCount().

### void QComboBox::setValidator ( const QValidator * v ) `[virtual]`

Sets this combo box to be editable only as allowed by v.

This function does nothing if the combo is not editable.

See also validator(), clearValidator() and QValidator.

### QSize QComboBox::sizeHint () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### int QComboBox::sizeLimit () const

Returns the current maximum on-screen size of the combo box. The default is ten lines.

See also setSizeLimit(), count() and maxCount().

### QSizePolicy QComboBox::sizePolicy () const `[virtual]`

Reimplemented for internal reasons; the API is not affected.

### void QComboBox::styleChange ( QStyle & s ) `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### QString QComboBox::text ( int index ) const

Returns the text item at a given index, or null string if the item is not a string.

See also currentText().

### void QComboBox::textChanged ( const QString & string ) `[signal]`

This signal is useful for editable comboboxes. It is emitted whenever the contents of the text entry field changes.

### void QComboBox::updateMask () `[virtual protected]`

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

### const QValidator * QComboBox::validator () const

Returns the validator which constrains editing for this combo box if there is any, or else 0.

See also setValidator(), clearValidator() and QValidator.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.