Qt 4.8: Q3ComboBox Class Reference

Qt Reference Documentation

- Qt 4.8
- ALL VERSIONS

- A
- A
- A
- Print

# Q3ComboBox Class Reference

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

     #include <Q3ComboBox>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherits: QWidget.

This class was introduced in Qt 4.1.

- List of all members, including inherited members
- Obsolete members

## Public Types

enum | Policy { NoInsertion, AtTop, AtCurrent, AtBottom, AfterCurrent, BeforeCurrent }

## Properties

autoCompletion : bool count : const int currentItem : int currentText : QString duplicatesEnabled : bool | editable : bool insertionPolicy : Policy maxCount : int sizeLimit : int

- 58 properties inherited from QWidget
- 1 property inherited from QObject

## Public Functions

| Q3ComboBox ( QWidget * parent = 0, const char * name = 0 )
| Q3ComboBox ( bool rw, QWidget * parent = 0, const char * name = 0 )
| ~Q3ComboBox ()
bool | autoCompletion () const
bool | autoResize () 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 )
int | count () const
int | currentItem () const
QString | currentText () const
bool | duplicatesEnabled () const
bool | editable () const
void | hide ()
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 | insertStrList ( const char ** strings, int numStrings = -1, int index = -1 )
void | insertStrList ( const Q3StrList & list, int index = -1 )
void | insertStrList ( const Q3StrList * list, int index = -1 )
void | insertStringList ( const QStringList & list, int index = -1 )
Policy | insertionPolicy () const
QLineEdit * | lineEdit () const
Q3ListBox * | listBox () const
int | maxCount () const
const QPixmap * | pixmap ( int index ) const
virtual void | popup ()
void | removeItem ( int index )
virtual void | setAutoCompletion ( bool )
virtual void | setAutoResize ( bool enable )
virtual void | setCurrentItem ( int index )
virtual void | setCurrentText ( const QString & )
void | setDuplicatesEnabled ( bool enable )
void | setEditable ( bool )
void | setEnabled ( bool enable )
void | setFont ( const QFont & font )
virtual void | setInsertionPolicy ( Policy policy )
virtual void | setLineEdit ( QLineEdit * edit )
virtual void | setListBox ( Q3ListBox * newListBox )
virtual void | setMaxCount ( int )
void | setPalette ( const QPalette & palette )
virtual void | setSizeLimit ( int )
virtual void | setValidator ( const QValidator * v )
int | sizeLimit () const
QString | text ( int index ) const
const QValidator * | validator () const

## Reimplemented Public Functions

virtual QSize | sizeHint () const

- 221 public functions inherited from QWidget
- 29 public functions inherited from QObject
- 13 public functions inherited from QPaintDevice

## Public Slots

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

- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject

## Signals

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

- 1 signal inherited from QWidget
- 1 signal inherited from QObject

## Protected Functions

void | updateMask ()

## Reimplemented Protected Functions

virtual void | focusInEvent ( QFocusEvent * e )
virtual void | focusOutEvent ( QFocusEvent * e )
virtual void | keyPressEvent ( QKeyEvent * e )
virtual void | mouseDoubleClickEvent ( QMouseEvent * e )
virtual void | mouseMoveEvent ( QMouseEvent * )
virtual void | mousePressEvent ( QMouseEvent * e )
virtual void | mouseReleaseEvent ( QMouseEvent * )
virtual void | paintEvent ( QPaintEvent * )
virtual void | resizeEvent ( QResizeEvent * e )
virtual void | wheelEvent ( QWheelEvent * e )

- 37 protected functions inherited from QWidget
- 8 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice

### Additional Inherited Members

- 4 static public members inherited from QWidget
- 7 static public members inherited from QObject
- 1 protected slot inherited from QWidget

## Detailed Description

The Q3ComboBox 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.

Comboboxes provide a means of showing the user's current choice out of a list of options in a way that takes up the minimum amount of screen space.

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

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

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

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

     Q3ComboBox *c1 = new Q3ComboBox(false, this, tr("read-only combobox"));
     Q3ComboBox *c2 = new Q3ComboBox(true, this, tr("editable 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 editable 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 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 setInsertionPolicy().

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

If the combobox 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 combobox 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().

Depending on the style, Q3ComboBox will use a list box or a popup menu to display the list of items. See setListBox() for more information.

{GUI Design Handbook}{GUI Design Handbook: Combo Box, Drop-Down List Box}

See also QComboBox, QLineEdit, and QSpinBox.

## Member Type Documentation

### enum Q3ComboBox::Policy

This enum specifies what the Q3ComboBox should do when a new string is entered by the user.

Constant | Value | Description
--- | --- | ---
`Q3ComboBox::NoInsertion` | `0` | the string will not be inserted into the combobox.
`Q3ComboBox::AtTop` | `1` | insert the string as the first item in the combobox.
`Q3ComboBox::AtCurrent` | `2` | replace the previously selected item with the string the user has entered.
`Q3ComboBox::AtBottom` | `3` | insert the string as the last item in the combobox.
`Q3ComboBox::AfterCurrent` | `4` | insert the string after the previously selected item.
`Q3ComboBox::BeforeCurrent` | `5` | 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.

## Property Documentation

### autoCompletion : bool

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.

Access functions:

bool | autoCompletion () const
virtual void | setAutoCompletion ( bool )

### count : const int

This property holds the number of items in the combobox.

Access functions:

int | count () const

### currentItem : int

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

Note that the activated() and highlighted() signals are only emitted when the user changes the current item, not when it is changed programmatically.

Access functions:

int | currentItem () const
virtual void | setCurrentItem ( int index )

### currentText : QString

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

Access functions:

QString | currentText () const
virtual void | setCurrentText ( const QString & )

### duplicatesEnabled : bool

This property holds whether duplicates are allowed.

If the combobox is editable and the user enters some text in the combobox's lineedit and presses Enter (and the insertionPolicy() is not `NoInsertion`), then what happens is this:

- 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 default), 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.

Access functions:

bool | duplicatesEnabled () const
void | setDuplicatesEnabled ( bool enable )

### editable : bool

This property holds whether the combobox is editable.

This property's default is false. Note that the combobox will be cleared if this property is set to true for a 1.x Motif style combobox. To avoid this, use setEditable() before inserting any items. Also note that the 1.x version of Motif didn't have any editable comboboxes, so the combobox will change its appearance to a 2.0 style Motif combobox is it is set to be editable.

Access functions:

bool | editable () const
void | setEditable ( bool )

### insertionPolicy : Policy

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

The default insertion policy is `AtBottom`. See Policy.

Access functions:

Policy | insertionPolicy () const
virtual void | setInsertionPolicy ( Policy policy )

### maxCount : int

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

Access functions:

int | maxCount () const
virtual void | setMaxCount ( int )

### sizeLimit : int

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

This property is ignored for both Motif 1.x style and non-editable comboboxes in Mac style. The default limit is ten lines. If the number of items in the combobox is or grows larger than lines, a scroll bar is added.

Access functions:

int | sizeLimit () const
virtual void | setSizeLimit ( int )

## Member Function Documentation

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

Constructs a combobox widget with parent parent called 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.

Note: If you use this constructor to create your Q3ComboBox, then the pixmap() function will always return 0. To workaround this, use the other constructor.

### Q3ComboBox::Q3ComboBox ( 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.

### Q3ComboBox::~Q3ComboBox ()

Destroys the combobox.

### void Q3ComboBox::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.

This signal is not emitted if the item is changed programmatically, e.g. using setCurrentItem().

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

This is an overloaded 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 Q3ComboBox::autoResize () const

Returns true if auto-resize is enabled; otherwise returns false.

See also setAutoResize() and autoResize.

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

Replaces the item at position index with the text t.

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

This is an overloaded function.

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

See also insertItem().

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

This is an overloaded function.

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

See also insertItem().

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

Removes all combobox items.

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

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

This is particularly useful 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 Enter is pressed.

See also setEditText().

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

This slot is equivalent to setValidator( 0 ).

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

Reimplemented from QWidget::focusInEvent().

### void Q3ComboBox::focusOutEvent ( QFocusEvent * e )`[virtual protected]`

Reimplemented from QWidget::focusOutEvent().

### void Q3ComboBox::hide ()

Hides the combobox.

See also QWidget::hide().

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

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

This signal is not emitted if the item is changed programmatically, e.g. using setCurrentItem().

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

This is an overloaded function.

This signal is emitted when a new item has been set to be the current item. string is the item's text.

You can also use the highlighted(int) signal.

### void Q3ComboBox::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.

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

This is an overloaded function.

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

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

This is an overloaded function.

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

### void Q3ComboBox::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 );

See also insertStringList().

### void Q3ComboBox::insertStrList ( const Q3StrList & list, int index = -1 )

This is an overloaded function.

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

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

### void Q3ComboBox::insertStrList ( const Q3StrList * list, int index = -1 )

This is an overloaded function.

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

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

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

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

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

Reimplemented from QWidget::keyPressEvent().

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

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

Only editable listboxes have a line editor.

See also setLineEdit().

### Q3ListBox * Q3ComboBox::listBox () const

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

See also setListBox().

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

Reimplemented from QWidget::mouseDoubleClickEvent().

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

Reimplemented from QWidget::mouseMoveEvent().

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

Reimplemented from QWidget::mousePressEvent().

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

Reimplemented from QWidget::mouseReleaseEvent().

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

Reimplemented from QWidget::paintEvent().

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

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

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

Pops up the combobox popup list.

If the list is empty, no items appear.

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

Removes the item at position index.

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

Reimplemented from QWidget::resizeEvent().

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

If enable is true, enable auto-resize; disable it otherwise.

See also autoResize.

### void Q3ComboBox::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().

### void Q3ComboBox::setEnabled ( bool enable )

Enables the combobox if enable is true; otherwise disables it.

See also QWidget::enabled.

### void Q3ComboBox::setFont ( const QFont & font )

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

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

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

See also lineEdit().

### void Q3ComboBox::setListBox ( Q3ListBox * 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: Q3ComboBox assumes that newListBox->text(n) returns non-null for 0 <= n < newListbox->count(). This assumption is necessary because of the line edit in Q3ComboBox.

See also listBox().

### void Q3ComboBox::setPalette ( const QPalette & palette )

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

### void Q3ComboBox::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 combobox is not editable.

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

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

Reimplemented from QWidget::sizeHint().

This implementation caches the size hint to avoid resizing when the contents change dynamically. To invalidate the cached value call setFont().

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

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

See also currentText().

### void Q3ComboBox::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.

### void Q3ComboBox::updateMask ()`[protected]`

Updates the widget mask.

See also QWidget::setMask().

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

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

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

### void Q3ComboBox::wheelEvent ( QWheelEvent * e )`[virtual protected]`

Reimplemented from QWidget::wheelEvent().