
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQ3ComboBox widget is a combined button and popup list.More...
| Header: | #include <Q3ComboBox> |
| Since: | Qt 4.1 |
| Inherits: | QWidget |
| enum | Policy { NoInsertion, AtTop, AtCurrent, AtBottom, AfterCurrent, BeforeCurrent } |
|
|
| 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 |
| virtual QSize | sizeHint() const |
| void | clear() |
| void | clearEdit() |
| void | clearValidator() |
| virtual void | setEditText(const QString & newText) |
| void | activated(int index) |
| void | activated(const QString & string) |
| void | highlighted(int index) |
| void | highlighted(const QString & string) |
| void | textChanged(const QString & string) |
| void | updateMask() |
| 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) |
TheQ3ComboBox 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=newQ3ComboBox(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=newQ3ComboBox(false,this, tr("read-only combobox"));Q3ComboBox*c2=newQ3ComboBox(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 withsizeLimit() andsetMaxCount() 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; theinsertItem() andchangeItem() functions are suitably overloaded. For editable comboboxes, the functionclearEdit() is provided, to clear the displayed string without changing the combobox's contents.
A combobox emits two signals,activated() andhighlighted(), when a new item has been activated (selected) or highlighted (made current). Both signals exist in two versions, one with aQString argument and one with anint argument. If the user highlights or activates a pixmap, only theint signals are emitted. Whenever the text of an editable combobox is changed thetextChanged() 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 isAtBottom but you can change this usingsetInsertionPolicy().
It is possible to constrain the input to an editable combobox usingQValidator; seesetValidator(). By default, any input is accepted.
If the combobox is not editable then it has a defaultfocusPolicy() ofTabFocus, 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 defaultfocusPolicy() ofStrongFocus, i.e. it will grab focus if clicked.
A combobox can be populated using the insert functions,insertStringList() andinsertItem() for example. Items can be changed withchangeItem(). An item can be removed withremoveItem() and all items can be removed withclear(). The text of the current item is returned bycurrentText(), and the text of a numbered item is returned withtext(). The current item can be set withsetCurrentItem() orsetCurrentText(). The number of items in the combobox is returned bycount(); the maximum number of items can be set withsetMaxCount(). You can allow editing usingsetEditable(). For editable comboboxes you can set auto-completion usingsetAutoCompletion() and whether or not the user can add duplicates is set withsetDuplicatesEnabled().
Depending on the style,Q3ComboBox will use a list box or a popup menu to display the list of items. SeesetListBox() for more information.
{GUI Design Handbook}{GUI Design Handbook: Combo Box, Drop-Down List Box}
See alsoQComboBox,QLineEdit, andQSpinBox.
This enum specifies what theQ3ComboBox 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.
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) |
This property holds the number of items in the combobox.
Access functions:
| int | count() const |
This property holds the index of the current item in the combobox.
Note that theactivated() andhighlighted() 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) |
This property holds the text of the combobox's current item.
Access functions:
| QString | currentText() const |
| virtual void | setCurrentText(const QString &) |
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 theinsertionPolicy() is notNoInsertion), then what happens is this:
This property only affects user-interaction. You can useinsertItem() to insert duplicates if you wish regardless of this setting.
Access functions:
| bool | duplicatesEnabled() const |
| void | setDuplicatesEnabled(bool enable) |
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) |
This property holds the position of the items inserted by the user.
The default insertion policy isAtBottom. SeePolicy.
Access functions:
| Policy | insertionPolicy() const |
| virtual void | setInsertionPolicy(Policy policy) |
This property holds the maximum number of items allowed in the combobox.
Access functions:
| int | maxCount() const |
| virtual void | setMaxCount(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) |
Constructs a combobox widget with parentparent calledname.
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 yourQ3ComboBox, then thepixmap() function will always return 0. To workaround this, use the other constructor.
Constructs a combobox with a maximum size and either Motif 2.0 or Windows look and feel.
The input field can be edited ifrw is true, otherwise the user may only choose one of the items in the combobox.
Theparent andname arguments are passed on to theQWidget constructor.
Destroys the combobox.
[signal]void Q3ComboBox::activated(int index)This signal is emitted when a new item has been activated (selected). Theindex is the position of the item in the combobox.
This signal is not emitted if the item is changed programmatically, e.g. usingsetCurrentItem().
Note:Signalactivated is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
connect(3ComboBox,static_cast<void(Q3ComboBox::*)(int)>(&Q3ComboBox::activated),[=](int index){/* ... */ });
[signal]void Q3ComboBox::activated(constQString & string)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.
Note:Signalactivated is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
connect(3ComboBox,static_cast<void(Q3ComboBox::*)(constQString&)>(&Q3ComboBox::activated),[=](constQString&string){/* ... */ });
Returns true if auto-resize is enabled; otherwise returns false.
See alsosetAutoResize() andautoResize.
Replaces the item at positionindex with the textt.
This is an overloaded function.
Replaces the item at positionindex with the pixmapim, unless the combobox is editable.
See alsoinsertItem().
This is an overloaded function.
Replaces the item at positionindex with the pixmapim and the textt.
See alsoinsertItem().
[slot]void Q3ComboBox::clear()Removes all combobox items.
[slot]void Q3ComboBox::clearEdit()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'sactivated() signal to clearEdit() in order to present the user with a new, empty line as soon as Enter is pressed.
See alsosetEditText().
[slot]void Q3ComboBox::clearValidator()This slot is equivalent tosetValidator( 0 ).
[virtual protected]void Q3ComboBox::focusInEvent(QFocusEvent * e)Reimplemented fromQWidget::focusInEvent().
[virtual protected]void Q3ComboBox::focusOutEvent(QFocusEvent * e)Reimplemented fromQWidget::focusOutEvent().
Hides the combobox.
See alsoQWidget::hide().
[signal]void Q3ComboBox::highlighted(int index)This signal is emitted when a new item has been set to be the current item. Theindex is the position of the item in the combobox.
This signal is not emitted if the item is changed programmatically, e.g. usingsetCurrentItem().
Note:Signalhighlighted is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
connect(3ComboBox,static_cast<void(Q3ComboBox::*)(int)>(&Q3ComboBox::highlighted),[=](int index){/* ... */ });
[signal]void Q3ComboBox::highlighted(constQString & string)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.
Note:Signalhighlighted is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
connect(3ComboBox,static_cast<void(Q3ComboBox::*)(constQString&)>(&Q3ComboBox::highlighted),[=](constQString&string){/* ... */ });
Inserts a text item with textt, at positionindex. The item will be appended ifindex is negative.
This is an overloaded function.
Inserts apixmap item at positionindex. The item will be appended ifindex is negative.
This is an overloaded function.
Inserts apixmap item with additional texttext at positionindex. The item will be appended ifindex is negative.
Inserts the array of char *strings at positionindex in the combobox.
ThenumStrings argument is the number of strings. IfnumStrings is -1 (default), thestrings array must be terminated with 0.
Example:
staticconstchar* items[]= {"red","green","blue",0 };combo->insertStrList( items );
See alsoinsertStringList().
This is an overloaded function.
Inserts thelist of strings at positionindex in the combobox.
This is only for compatibility since it does not support Unicode strings. SeeinsertStringList().
This is an overloaded function.
Inserts thelist of strings at positionindex in the combobox.
This is only for compatibility since it does not support Unicode strings. SeeinsertStringList().
Inserts thelist of strings at positionindex in the combobox.
[virtual protected]void Q3ComboBox::keyPressEvent(QKeyEvent * e)Reimplemented fromQWidget::keyPressEvent().
Returns the line edit, or 0 if there is no line edit.
Only editable listboxes have a line editor.
See alsosetLineEdit().
Returns the current list box, or 0 if there is no list box. (Q3ComboBox can useQPopupMenu instead ofQListBox.) Provided to matchsetListBox().
See alsosetListBox().
[virtual protected]void Q3ComboBox::mouseDoubleClickEvent(QMouseEvent * e)Reimplemented fromQWidget::mouseDoubleClickEvent().
[virtual protected]void Q3ComboBox::mouseMoveEvent(QMouseEvent *)Reimplemented fromQWidget::mouseMoveEvent().
[virtual protected]void Q3ComboBox::mousePressEvent(QMouseEvent * e)Reimplemented fromQWidget::mousePressEvent().
[virtual protected]void Q3ComboBox::mouseReleaseEvent(QMouseEvent *)Reimplemented fromQWidget::mouseReleaseEvent().
[virtual protected]void Q3ComboBox::paintEvent(QPaintEvent *)Reimplemented fromQWidget::paintEvent().
Returns the pixmap item at positionindex, or 0 if the item is not a pixmap.
[virtual]void Q3ComboBox::popup()Pops up the combobox popup list.
If the list is empty, no items appear.
Removes the item at positionindex.
[virtual protected]void Q3ComboBox::resizeEvent(QResizeEvent * e)Reimplemented fromQWidget::resizeEvent().
[virtual]void Q3ComboBox::setAutoResize(bool enable)Ifenable is true, enable auto-resize; disable it otherwise.
See alsoautoResize.
[virtual slot]void Q3ComboBox::setEditText(constQString & newText)Sets the text in the line edit tonewText 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 alsoclearEdit() andinsertItem().
Enables the combobox ifenable is true; otherwise disables it.
See alsoQWidget::enabled.
Sets the font for both the combobox button and the combobox popup list tofont.
[virtual]void Q3ComboBox::setLineEdit(QLineEdit * edit)Sets the line edit to useedit instead of the current line edit.
See alsolineEdit().
[virtual]void Q3ComboBox::setListBox(Q3ListBox * newListBox)Sets the combobox to usenewListBox 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 inQ3ComboBox.
See alsolistBox().
Sets the palette for both the combobox button and the combobox popup list topalette.
[virtual]void Q3ComboBox::setValidator(constQValidator * v)Applies the validatorv to the combobox so that only text which is valid according tov is accepted.
This function does nothing if the combobox is not editable.
See alsovalidator(),clearValidator(), andQValidator.
[virtual]QSize Q3ComboBox::sizeHint() constReimplemented fromQWidget::sizeHint().
This implementation caches the size hint to avoid resizing when the contents change dynamically. To invalidate the cached value callsetFont().
Returns the text item at positionindex, orQString::null if the item is not a string.
See alsocurrentText().
[signal]void Q3ComboBox::textChanged(constQString & string)This signal is used for editable comboboxes. It is emitted whenever the contents of the text entry field changes.string contains the new text.
[protected]void Q3ComboBox::updateMask()Updates the widget mask.
See alsoQWidget::setMask().
Returns the validator which constrains editing for this combobox if there is one; otherwise returns 0.
See alsosetValidator(),clearValidator(), andQValidator.
[virtual protected]void Q3ComboBox::wheelEvent(QWheelEvent * e)Reimplemented fromQWidget::wheelEvent().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.