QComboBox(3qt)
π NAME π
QComboBox β bestie, it's a combined button and popup list. That's the whole vibe. This is the manual page for QComboBox(3qt) and we're gonna get into it π
β¨ SYNOPSIS β¨
#include <qcombobox.h>
Inherits QWidget.
πΈ 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 enabledbool 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 comboboxQString currentTextβ the text of the combobox's current itembool duplicatesEnabledβ whether duplicates are allowedbool editableβ whether the combobox is editablePolicy insertionPolicyβ the position of the items inserted by the userint maxCountβ the maximum number of items allowed in the comboboxint sizeLimitβ the maximum on-screen size of the combobox
π DESCRIPTION π
The QComboBox widget is a combined button and popup list. Yes, literally both. It's giving multitasking queen β¨
So a combobox is a selection widget that shows you the current item and can pop up a whole list of items when you ask it to. And a combobox may be editable, in which case the user can just... type arbitrary strings in. Iconic behaviour honestly.
Comboboxes are how you show the user's current choice out of a list of options in a way that takes up the minimum amount of screen space. Small footprint, big opinions.
QComboBox supports three different display styles: Aqua/Motif 1.x, Motif 2.0 and Windows. History lesson time: in Motif 1.x, a combobox was called XmOptionMenu. In Motif 2.0, OSF introduced an improved combobox and named that one XmComboBox. QComboBox provides both. Versatile π
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 it can create both read-only and editable comboboxes:
QComboBox *c1 = new QComboBox( FALSE, this, "read-only combobox" );
QComboBox *c2 = new QComboBox( TRUE, this, "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. Ma'am. It has no chill. π
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. Sneaky but useful π«Ά
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. And whenever the text of an editable combobox is changed, the textChanged() signal is emitted. So many signals, so much communication β¨
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 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. Anything. Wild.
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. Pick your fighter.
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(). Okay that was a lot but every single one of those is a real function, I'm not making them up π
[Image Omitted]
(Motif 1, read-only)
[Image Omitted]
(Motif 2, editable)
[Image Omitted]
(Motif 2, read-only)
[Image Omitted]
(Windows style)
Depending on the style, QComboBox will use a QListBox or a QPopupMenu to display the list of items. See setListBox() for more information.
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. Six options, six whole personalities:
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. No matter what. Periodt.
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. So yeah, something's getting deleted, but which end is a mystery π
π MEMBER FUNCTION DOCUMENTATION π
QComboBox::QComboBox ( 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 yourQComboBox, then thepixmap()function will always return0. To workaround this, use the other constructor. Noted, bestie π
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. π RIP boo, you did amazing sweetie.
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.
This signal is not emitted if the item is changed programmatically, e.g. using setCurrentItem().
Examples:
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. Important distinction, don't get caught lacking π«Ά
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().
more of this page is coming, bestie, we're only on part 1 of 3 β don't go anywhere π
void QComboBox::changeItem ( const QPixmap & im, const QString & t, int index )
okay bestie this one is an overloaded member function, it's here purely for convenience, and it behaves essentially like the one right above it. π
What it does: it replaces the item sitting at position index with the pixmap im and the text t. Both. Together. Slay.
See also insertItem().
void QComboBox::clear () [slot]
Removes all the combobox items. every single one. gone. π
void QComboBox::clearEdit () [slot]
Clears the line edit without changing the combobox's contents. And if the combobox isn't editable? It does nothing. literally nothing. π
This one is particularly useful when you're using a combobox as a line edit with history. For example, you can connect the combobox's activated() signal to clearEdit() so that the user gets a fresh, empty line the moment Enter is pressed. obsessed. β¨
See also setEditText().
void QComboBox::clearValidator () [slot]
This slot is the equivalent of setValidator( 0 ). that's it, that's the whole vibe π
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 goes off when a new item has been set to be the current item. The index is the position of that item in the combobox. π«Ά
Important: this signal is not emitted if the item is changed programmatically, e.g. by using setCurrentItem(). so if you're sitting there like "why isn't my signal firing" β that's why bestie.
void QComboBox::highlighted ( const QString & string ) [signal]
Overloaded member function, provided for convenience, behaves essentially like the one above. π
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 QComboBox::insertItem ( const QString & t, int index = -1 )
Inserts a text item with text t, at position index. If index is negative, the item just gets appended. no drama. β¨
Examples:
void QComboBox::insertItem ( const QPixmap & pixmap, int index = -1 )
Overloaded member function, provided for convenience, 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 )
Overloaded member function, provided for convenience, 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 (the default), then the strings array must be terminated with 0. don't forget the 0 bestie, it's giving "forgot the null terminator" energy π
Example:
static const char* items[] = { "red", "green", "blue", 0 };
combo->insertStrList( items );
See also insertStringList().
Example: qmag/qmag.cpp.
void QComboBox::insertStrList ( const QStrList & list, int index = -1 )
Overloaded member function, provided for convenience, behaves essentially like the above 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 QComboBox::insertStrList ( const QStrList * list, int index = -1 )
Overloaded member function, provided for convenience, behaves essentially like the above 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 QComboBox::insertStringList ( const QStringList & list, int index = -1 )
Inserts the list of strings at position index in the combobox. this is the Unicode-friendly one, the main character of the insertStrList trio π
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 edit, or 0 if there is no line edit.
Only editable listboxes have a line editor. iykyk β¨
QListBox * QComboBox::listBox () const
Returns the current list box, or 0 if there is no list box. (Because QComboBox can use QPopupMenu instead of QListBox.) It's 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. (obviously, but they said it so we say it)
void QComboBox::removeItem ( int index )
Removes the item at position index. bye bestie π«Ά
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 giving the user a good starting point for their editing, and only entering the change in the combobox when the user presses Enter.
See also clearEdit() and insertItem().
void QComboBox::setEditable ( bool )
Sets whether the combobox is editable. See the "editable" property for details.
void QComboBox::setFont ( const QFont & font ) [virtual]
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 line edit to use edit instead of the current line edit. π
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. yeah, it wipes everything, so heads up π«Ά
Warning:QComboBoxassumes thatnewListBox->text(n)returns non-null for0 <= n < newListbox->count(). This assumption is necessary because of the line edit inQComboBox.
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]
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 combobox 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 QString::null if the item is not a string.
See also currentText.
Examples:
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.
Note that the activated() and highlighted() signals are only emitted when the user changes the current item, not when it is changed programmatically.
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.
Okay so bestie, if the combobox is editable and the user types some text in the combobox's lineedit and presses Enter (and the insertionPolicy() is not NoInsertion), then here's what goes down:
- If the text is not already in the list, the text is inserted. Slay.
- If the text is in the list and this property is
TRUE(the default), the text is inserted. Double slay. - 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. No new item for you, periodt.
This property only affects user-interaction. You can use insertItem() to insert duplicates if you wish regardless of this setting. So you do you, boo.
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.
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 it's appearance to a 2.0 style Motif combobox is it is set to be editable. (Yes, that's exactly what the original says β we're just keeping it 100. π―)
π¨ Bestie warning: if you set this to TRUE on a 1.x Motif style combobox without setting editable first, you're gonna have a bad time and lose all your items. Don't do it. π
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. See Policy.
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 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 scrollbar is added.
Set this property's value with setSizeLimit() and get this property's value with sizeLimit().
π SEE ALSO π
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
βοΈ AUTHOR βοΈ
Generated automatically from the source code. (No cap, the robots did it. π€)
π BUGS π
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. π«Ά
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qcombobox.3qt) and the Qt version (3.3.8).
And that's a wrap, bestie! This iconic page was brought to you by Trolltech AS, dated 2 February 2007, for QComboBox(3qt). Stay slay, keep coding, and remember: duplicates are a choice, but reading the man page is mandatory. π β¨π
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms