Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# 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 & list, int index = -1 )
- void insertStrList ( const QStrList & list, int index = -1 )
- void insertStrList ( const QStrList * list, int index = -1 )
- void insertStrList ( const char ** strings, int numStrings = -1, int index = -1 )
- void insertItem ( const QString & t, 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 )
- int currentItem () const
- virtual void setCurrentItem ( int index )
- QString currentText () const
- virtual void setCurrentText ( const QString & )
- QString text ( int index ) const
- const QPixmap * pixmap ( int index ) const
- void changeItem ( const QString & t, int index )
- void changeItem ( const QPixmap & im, int index )
- void changeItem ( const QPixmap & im, const QString & t, int index )
- bool autoResize () const (obsolete)
- virtual void setAutoResize ( bool ) (obsolete)
- virtual void setPalette ( const QPalette & palette )
- virtual void setFont ( const QFont & font )
- 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 * v )
- const QValidator * validator () const
- virtual void setListBox ( QListBox * newListBox )
- QListBox * listBox () const
- virtual void setLineEdit ( QLineEdit * edit )
- QLineEdit * lineEdit () const
- virtual void setAutoCompletion ( bool )
- bool autoCompletion () const
- void setDuplicatesEnabled ( bool enable )
- bool duplicatesEnabled () const
- bool editable () const
- void setEditable ( bool )
- virtual void popup ()

## Public Slots

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

## Signals

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

## Properties

- bool autoCompletion - whether auto-completion is enabled
- bool autoMask - whether the combobox is automatically masked (read only)
- bool autoResize - whether auto resize is enabled (obsolete)
- int count - the number of items in the combobox (read only)
- int currentItem - the index of the current item in the combobox
- QString currentText - the text of the combobox's current item
- bool duplicatesEnabled - whether duplicates are allowed
- bool editable - whether the combobox is editable
- Policy insertionPolicy - the position of the items inserted by the user
- int maxCount - the maximum number of items allowed in the combobox
- int sizeLimit - the maximum on-screen size of the combobox

## Detailed Description

The QComboBox widget is a combined button and popup list.

A combobox is a selection widget which displays the current item and can pop up a list of items. A combobox may be editable in which case the user can enter arbitrary strings.

Since comboboxes occupy little screen space and always display the current item, they are well suited to displaying items that the user will want to see, such as font family or size. Using a combobox the user can always see which item they've selected with the minimum amount of screen space being used.

QComboBox supports three different display styles: Aqua/Motif 1.x, Motif 2.0 and Windows 95. In Motif 1.x, a combobox was called XmOptionMenu. In Motif 2.0, OSF introduced an improved combobox and named that XmComboBox. QComboBox provides both.

QComboBox provides two different constructors. The simplest constructor creates an old-style combobox in Motif (or Aqua) style:

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

The other constructor creates a new-style combobox in Motif style, and can create both read-only and read-write comboboxes:

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

New-style comboboxes 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 with sizeLimit() and setMaxCount() respectively. Old-style comboboxes use a popup in Aqua and Motif style, and that popup will happily grow larger than the desktop if you put enough data into it.

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

Comboboxes can contain pixmaps as well as strings; the insertItem() and changeItem() functions are suitably overloaded. For read-write comboboxes, the function clearEdit() is provided, to clear the displayed string without changing the combobox's contents.

A combobox emits two signals, activated() and highlighted(), when a new item has been activated (selected) or highlighted (made current). Both signals exist in two versions, one with a QString argument and one with an `int` argument. If the user highlights or activates a pixmap, only the `int` signals are emitted. Whenever the text of an editable combobox is changed the textChanged() signal is emitted.

When the user enters a new string in a read-write 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 setInsertionPolicy().

It is possible to constrain the input to an editable combobox 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.

A combobox can be populated using the insert functions, insertStringList() and insertItem() for example. Items can be changed with changeItem(). 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 setCurrentItem() or setCurrentText(). 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 setAutoCompletion() and whether or not the user can add duplicates is set with setDuplicatesEnabled().

(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, GUI Design Handbook: Drop-Down List Box and Basic Widgets.

## Member Type Documentation

### QComboBox::Policy

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

- `QComboBox::NoInsertion` - the string will not be inserted into the combobox.
- `QComboBox::AtTop` - insert the string as the first item in the combobox.
- `QComboBox::AtCurrent` - replace the previously selected item with the string the user has entered.
- `QComboBox::AtBottom` - insert the string as the last item in the combobox.
- `QComboBox::AfterCurrent` - insert the string after the previously selected item.
- `QComboBox::BeforeCurrent` - insert the string before the previously selected item.

activated() is always emitted when the string is entered.

If inserting the new string would cause the combobox 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 combobox widget with parent parent and name name.

This constructor creates a popup list if the program uses Motif (or Aqua) look and feel; this is compatible with Motif 1.x and Aqua.

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

Constructs a combobox 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 combobox.

The parent and name arguments are passed on to the QWidget constructor.

### QComboBox::~QComboBox ()

Destroys the combobox.

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

Examples: fileiconview/mainwindow.cpp, helpviewer/helpwindow.cpp, lineedits/lineedits.cpp, listboxcombo/listboxcombo.cpp, network/ftpclient/ftpmainwindow.cpp and qmag/qmag.cpp.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

### bool QComboBox::autoCompletion () const

Returns TRUE if auto-completion is enabled; otherwise returns FALSE. See the "autoCompletion" property for details.

### bool QComboBox::autoResize () const

Returns TRUE if auto resize is enabled; otherwise returns FALSE. See the "autoResize" property for details.

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

Replaces the item at position index with the text t.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Replaces the item at position index with the pixmap im, unless the combobox is editable.

See also insertItem().

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Replaces the item at position index with the pixmap im and the text t.

See also insertItem().

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

Removes all combobox items.

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

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

This is particularly handy when using a combobox as a line edit with history. For example you can connect the combobox'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 combobox. See the "count" property for details.

### int QComboBox::currentItem () const

Returns the index of the current item in the combobox. See the "currentItem" property for details.

### QString QComboBox::currentText () const

Returns the text of the combobox's current item. See the "currentText" property for details.

### bool QComboBox::duplicatesEnabled () const

Returns TRUE if duplicates are allowed; otherwise returns FALSE. See the "duplicatesEnabled" property for details.

### bool QComboBox::editable () const

Returns TRUE if the combobox is editable; otherwise returns FALSE. See the "editable" property for details.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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::insertItem ( const QString & t, int index = -1 )

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

Examples: fileiconview/mainwindow.cpp, helpviewer/helpwindow.cpp, lineedits/lineedits.cpp, listboxcombo/listboxcombo.cpp, network/ftpclient/ftpmainwindow.cpp and tictac/tictac.cpp.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

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

Inserts the array of char * strings at position index in the combobox.

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

Example:

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

Example: qmag/qmag.cpp.

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts the list of strings at position index in the combobox.

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

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

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Inserts the list of strings at position index in the combobox.

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

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

Inserts the list of strings at position index in the combobox.

### Policy QComboBox::insertionPolicy () const

Returns the position of the items inserted by the user. See the "insertionPolicy" property for details.

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

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

Only editable listboxes have a line editor.

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

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

See also setListBox().

Example: listboxcombo/listboxcombo.cpp.

### int QComboBox::maxCount () const

Returns the maximum number of items allowed in the combobox. See the "maxCount" property for details.

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

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

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

Pops up the combobox popup list.

If the list is empty, no items appear.

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

Removes the item at position index.

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

Sets whether auto-completion is enabled. See the "autoCompletion" property for details.

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

Sets whether auto resize is enabled. See the "autoResize" property for details.

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

Sets the index of the current item in the combobox to index. See the "currentItem" property for details.

### void QComboBox::setCurrentText ( const QString & )`[virtual]`

Sets the text of the combobox's current item. See the "currentText" property for details.

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

Sets whether duplicates are allowed to enable. See the "duplicatesEnabled" property for details.

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

Sets the text in the line edit to newText without changing the combobox's contents. Does nothing if the combobox 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 combobox only when the user presses Enter.

See also clearEdit() and insertItem().

Example: network/ftpclient/ftpmainwindow.cpp.

### void QComboBox::setEditable ( bool )

Sets whether the combobox is editable. See the "editable" property for details.

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

Reimplements QWidget::setFont().

Sets the font for both the combobox button and the combobox popup list to font.

Reimplemented from QWidget.

### void QComboBox::setInsertionPolicy ( Policy policy )`[virtual]`

Sets the position of the items inserted by the user to policy. See the "insertionPolicy" property for details.

### void QComboBox::setLineEdit ( QLineEdit * edit )`[virtual]`

Sets the lineedit to use edit instead of the current lineedit.

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

Sets the combobox to use newListBox instead of the current list box or popup. As a side effect, it clears the combobox 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 )`[virtual]`

Sets the maximum number of items allowed in the combobox. See the "maxCount" property for details.

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

Reimplements QWidget::setPalette().

Sets the palette for both the combobox button and the combobox popup list to palette.

Reimplemented from QWidget.

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

Sets the maximum on-screen size of the combobox. See the "sizeLimit" property for details.

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

Applies the validator v to the combobox so that only text which is valid according to v is accepted.

This function does nothing if the combo is not editable.

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

### int QComboBox::sizeLimit () const

Returns the maximum on-screen size of the combobox. See the "sizeLimit" property for details.

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

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

See also currentText.

Examples: fileiconview/mainwindow.cpp and helpviewer/helpwindow.cpp.

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

This signal is used for editable comboboxes. It is emitted whenever the contents of the text entry field changes. string contains the new text.

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

Returns the validator which constrains editing for this combobox if there is one, otherwise returns 0.

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

## Property Documentation

### bool autoCompletion

This property holds whether auto-completion is enabled.

This property can only be set for editable comboboxes, for non-editable comboboxes it has no effect. It is FALSE by default.

Set this property's value with setAutoCompletion() and get this property's value with autoCompletion().

### bool autoMask

This property holds whether the combobox is automatically masked.

See also QWidget::autoMask.

### bool autoResize

This property holds whether auto resize is enabled.

This property is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

If this property is set to TRUE then the combobox will resize itself whenever its contents change. The default is FALSE.

Set this property's value with setAutoResize() and get this property's value with autoResize().

### int count

This property holds the number of items in the combobox.

Get this property's value with count().

### int currentItem

This property holds the index of the current item in the combobox.

Set this property's value with setCurrentItem() and get this property's value with currentItem().

### QString currentText

This property holds the text of the combobox's current item.

Set this property's value with setCurrentText() and get this property's value with currentText().

### bool duplicatesEnabled

This property holds whether duplicates are allowed.

If the combobox is editable and the user enters some text in the lineedit of the combobox and presses Enter (and the insertionPolicy() is different from NoInsertion), then what happens is as follows:

- If the text is not already in the list, the text is inserted.
- If the text is in the list and this property is TRUE, the text is inserted.
- If the text is in the list and this property is FALSE, the text is not inserted; instead the item which has matching text becomes the current item.

This property only affects user-interaction. You can use insertItem() to insert duplicates if you wish regardless of this setting.

Set this property's value with setDuplicatesEnabled() and get this property's value with duplicatesEnabled().

### bool editable

This property holds whether the combobox is editable.

Set this property's value with setEditable() and get this property's value with editable().

### Policy insertionPolicy

This property holds the position of the items inserted by the user.

The default insertion policy is AtBottom.

Set this property's value with setInsertionPolicy() and get this property's value with insertionPolicy().

### int maxCount

This property holds the maximum number of items allowed in the combobox.

Set this property's value with setMaxCount() and get this property's value with maxCount().

### int sizeLimit

This property holds the maximum on-screen size of the combobox.

This is disregarded in Motif 1.x style. The default limit is ten lines. If the number of items in the combobox is or grows larger than lines, a scrollbar is added.

Set this property's value with setSizeLimit() and get this property's value with sizeLimit().

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