Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qcombobox.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QCOMBOBOX_H
5#define QCOMBOBOX_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtWidgets/qwidget.h>
9#include <QtWidgets/qabstractitemdelegate.h>
10#include <QtCore/qabstractitemmodel.h>
11#include <QtCore/qvariant.h>
12#include <QtGui/qvalidator.h>
13
15
17
19class QLineEdit;
21class QCompleter;
22
23class Q_WIDGETS_EXPORT QComboBox : public QWidget
24{
26
27 Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
28 Q_PROPERTY(int count READ count)
29 Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText NOTIFY currentTextChanged
30 USER true)
31 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
32 Q_PROPERTY(QVariant currentData READ currentData)
33 Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems)
34 Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount)
35 Q_PROPERTY(InsertPolicy insertPolicy READ insertPolicy WRITE setInsertPolicy)
36 Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy)
37 Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
38 Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
39 Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
40 Q_PROPERTY(bool duplicatesEnabled READ duplicatesEnabled WRITE setDuplicatesEnabled)
41 Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
42 Q_PROPERTY(int modelColumn READ modelColumn WRITE setModelColumn)
43
44public:
45 explicit QComboBox(QWidget *parent = nullptr);
46 ~QComboBox();
47
48 int maxVisibleItems() const;
49 void setMaxVisibleItems(int maxItems);
50
51 int count() const;
52 void setMaxCount(int max);
53 int maxCount() const;
54
55 bool duplicatesEnabled() const;
56 void setDuplicatesEnabled(bool enable);
57
58 void setFrame(bool);
59 bool hasFrame() const;
60
61 inline int findText(const QString &text,
62 Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
63 { return findData(text, Qt::DisplayRole, flags); }
64 int findData(const QVariant &data, int role = Qt::UserRole,
65 Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const;
66
74 InsertAlphabetically
75 };
76 Q_ENUM(InsertPolicy)
77
78 InsertPolicy insertPolicy() const;
79 void setInsertPolicy(InsertPolicy policy);
80
84 AdjustToMinimumContentsLengthWithIcon
85 };
86 Q_ENUM(SizeAdjustPolicy)
87
88 SizeAdjustPolicy sizeAdjustPolicy() const;
89 void setSizeAdjustPolicy(SizeAdjustPolicy policy);
90 int minimumContentsLength() const;
91 void setMinimumContentsLength(int characters);
92 QSize iconSize() const;
93 void setIconSize(const QSize &size);
94
95 void setPlaceholderText(const QString &placeholderText);
96 QString placeholderText() const;
97
98 bool isEditable() const;
99 void setEditable(bool editable);
100 void setLineEdit(QLineEdit *edit);
101 QLineEdit *lineEdit() const;
102#ifndef QT_NO_VALIDATOR
103 void setValidator(const QValidator *v);
104 const QValidator *validator() const;
105#endif
106
107#if QT_CONFIG(completer)
109 QCompleter *completer() const;
110#endif
111
112 QAbstractItemDelegate *itemDelegate() const;
113 void setItemDelegate(QAbstractItemDelegate *delegate);
114
115 QAbstractItemModel *model() const;
116 virtual void setModel(QAbstractItemModel *model);
117
118 QModelIndex rootModelIndex() const;
119 void setRootModelIndex(const QModelIndex &index);
120
121 int modelColumn() const;
122 void setModelColumn(int visibleColumn);
123
124 int currentIndex() const;
125 QString currentText() const;
126 QVariant currentData(int role = Qt::UserRole) const;
127
128 QString itemText(int index) const;
129 QIcon itemIcon(int index) const;
130 QVariant itemData(int index, int role = Qt::UserRole) const;
131
132 inline void addItem(const QString &text, const QVariant &userData = QVariant());
133 inline void addItem(const QIcon &icon, const QString &text,
134 const QVariant &userData = QVariant());
135 inline void addItems(const QStringList &texts)
136 { insertItems(count(), texts); }
137
138 inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
139 void insertItem(int index, const QIcon &icon, const QString &text,
140 const QVariant &userData = QVariant());
141 void insertItems(int index, const QStringList &texts);
142 void insertSeparator(int index);
143
144 void removeItem(int index);
145
146 void setItemText(int index, const QString &text);
147 void setItemIcon(int index, const QIcon &icon);
148 void setItemData(int index, const QVariant &value, int role = Qt::UserRole);
149
150 QAbstractItemView *view() const;
151 void setView(QAbstractItemView *itemView);
152
153 QSize sizeHint() const override;
154 QSize minimumSizeHint() const override;
155
156 virtual void showPopup();
157 virtual void hidePopup();
158
159 bool event(QEvent *event) override;
162
163public Q_SLOTS:
164 void clear();
165 void clearEditText();
166 void setEditText(const QString &text);
167 void setCurrentIndex(int index);
168 void setCurrentText(const QString &text);
169
172 void activated(int index);
173 void textActivated(const QString &);
178
179protected:
180 void focusInEvent(QFocusEvent *e) override;
181 void focusOutEvent(QFocusEvent *e) override;
182 void changeEvent(QEvent *e) override;
183 void resizeEvent(QResizeEvent *e) override;
184 void paintEvent(QPaintEvent *e) override;
185 void showEvent(QShowEvent *e) override;
186 void hideEvent(QHideEvent *e) override;
187 void mousePressEvent(QMouseEvent *e) override;
188 void mouseReleaseEvent(QMouseEvent *e) override;
189 void keyPressEvent(QKeyEvent *e) override;
190 void keyReleaseEvent(QKeyEvent *e) override;
191#if QT_CONFIG(wheelevent)
192 void wheelEvent(QWheelEvent *e) override;
193#endif
194#ifndef QT_NO_CONTEXTMENU
195 void contextMenuEvent(QContextMenuEvent *e) override;
196#endif // QT_NO_CONTEXTMENU
197 void inputMethodEvent(QInputMethodEvent *) override;
198 virtual void initStyleOption(QStyleOptionComboBox *option) const;
199
200
201protected:
203
204private:
205 Q_DECLARE_PRIVATE(QComboBox)
206 Q_DISABLE_COPY(QComboBox)
207 Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item))
208 Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &))
209 Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index))
210 Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
211 Q_PRIVATE_SLOT(d_func(), void _q_returnPressed())
212 Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
213 Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &))
214 Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange())
215 Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end))
216 Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end))
217 Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
218 Q_PRIVATE_SLOT(d_func(), void _q_modelReset())
219#if QT_CONFIG(completer)
220 Q_PRIVATE_SLOT(d_func(), void _q_completerActivated(const QModelIndex &index))
221#endif
222};
223
224inline void QComboBox::addItem(const QString &atext, const QVariant &auserData)
225{ insertItem(count(), atext, auserData); }
226inline void QComboBox::addItem(const QIcon &aicon, const QString &atext,
227 const QVariant &auserData)
228{ insertItem(count(), aicon, atext, auserData); }
229
230inline void QComboBox::insertItem(int aindex, const QString &atext,
231 const QVariant &auserData)
232{ insertItem(aindex, QIcon(), atext, auserData); }
233
235
236#endif // QCOMBOBOX_H
The QAbstractItemDelegate class is used to display and edit data items from a model.
The QAbstractItemView class provides the basic functionality for item view classes.
The QComboBox widget is a combined button and popup list.
Definition qcombobox.h:24
InsertPolicy
This enum specifies what the QComboBox should do when a new string is entered by the user.
Definition qcombobox.h:67
@ InsertAtCurrent
Definition qcombobox.h:70
@ InsertAtTop
Definition qcombobox.h:69
@ InsertBeforeCurrent
Definition qcombobox.h:73
@ InsertAfterCurrent
Definition qcombobox.h:72
@ InsertAtBottom
Definition qcombobox.h:71
SizeAdjustPolicy
This enum specifies how the size hint of the QComboBox should adjust when new content is added or con...
Definition qcombobox.h:81
@ AdjustToContents
Definition qcombobox.h:82
@ AdjustToContentsOnFirstShow
Definition qcombobox.h:83
void insertItem(int index, const QString &text, const QVariant &userData=QVariant())
Inserts the text and userData (stored in the Qt::UserRole) into the combobox at the given index.
Definition qcombobox.h:230
void activated(int index)
This signal is sent when the user chooses an item in the combobox.
int count
the number of items in the combobox
Definition qcombobox.h:28
void editTextChanged(const QString &)
This signal is emitted when the text in the combobox's line edit widget is changed.
void currentTextChanged(const QString &)
void addItem(const QString &text, const QVariant &userData=QVariant())
Adds an item to the combobox with the given text, and containing the specified userData (stored in th...
Definition qcombobox.h:224
void currentIndexChanged(int index)
void textActivated(const QString &)
void addItems(const QStringList &texts)
Adds each of the strings in the given texts to the combobox.
Definition qcombobox.h:135
void textHighlighted(const QString &)
int findText(const QString &text, Qt::MatchFlags flags=static_cast< Qt::MatchFlags >(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
Returns the index of the item containing the given text; otherwise returns -1.
Definition qcombobox.h:61
void highlighted(int index)
This signal is sent when an item in the combobox popup list is highlighted by the user.
The QCompleter class provides completions based on an item model.
Definition qcompleter.h:24
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition qevent.h:593
\inmodule QtCore
Definition qcoreevent.h:45
The QFocusEvent class contains event parameters for widget focus events.
Definition qevent.h:469
The QHideEvent class provides an event which is sent after a widget is hidden.
Definition qevent.h:585
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
The QInputMethodEvent class provides parameters for input method events.
Definition qevent.h:624
The QKeyEvent class describes a key event.
Definition qevent.h:423
The QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
\inmodule QtCore
\inmodule QtGui
Definition qevent.h:195
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:547
The QShowEvent class provides an event that is sent when a widget is shown.
Definition qevent.h:577
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\variable QStyleOptionToolButton::features
The QValidator class provides validation of input text.
Definition qvalidator.h:24
\inmodule QtCore
Definition qvariant.h:64
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
virtual void hideEvent(QHideEvent *event)
This event handler can be reimplemented in a subclass to receive widget hide events.
virtual void mousePressEvent(QMouseEvent *event)
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
Definition qwidget.cpp:9529
virtual void focusInEvent(QFocusEvent *event)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
Definition qwidget.cpp:9711
virtual void mouseReleaseEvent(QMouseEvent *event)
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
Definition qwidget.cpp:9554
virtual void keyReleaseEvent(QKeyEvent *event)
This event handler, for event event, can be reimplemented in a subclass to receive key release events...
Definition qwidget.cpp:9687
QSize minimumSizeHint
the recommended minimum size for the widget
Definition qwidget.h:149
virtual QVariant inputMethodQuery(Qt::InputMethodQuery) const
This method is only relevant for input widgets.
Definition qwidget.cpp:9959
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition qwidget.cpp:9428
QSize sizeHint
the recommended size for the widget
Definition qwidget.h:148
virtual void inputMethodEvent(QInputMethodEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive Input Method compo...
Definition qwidget.cpp:9944
virtual void focusOutEvent(QFocusEvent *event)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
Definition qwidget.cpp:9737
virtual void resizeEvent(QResizeEvent *event)
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
Definition qwidget.cpp:9868
virtual void keyPressEvent(QKeyEvent *event)
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
Definition qwidget.cpp:9652
virtual void paintEvent(QPaintEvent *event)
This event handler can be reimplemented in a subclass to receive paint events passed in event.
Definition qwidget.cpp:9829
virtual void showEvent(QShowEvent *event)
This event handler can be reimplemented in a subclass to receive widget show events which are passed ...
virtual void contextMenuEvent(QContextMenuEvent *event)
This event handler, for event event, can be reimplemented in a subclass to receive widget context men...
Definition qwidget.cpp:9921
b clear()
QString text
double e
opt iconSize
Combined button and popup list for selecting options.
InputMethodQuery
@ UserRole
@ DisplayRole
@ MatchCaseSensitive
@ MatchExactly
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLsizei const GLfloat * v
[13]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLuint GLuint end
GLenum GLenum GLsizei count
GLbitfield flags
GLboolean enable
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
struct _cl_event * event
GLenum query
const GLubyte * c
GLsizei maxCount
Definition qopenglext.h:677
GLuint GLenum option
#define QT_REQUIRE_CONFIG(feature)
#define Q_ENUM(x)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_INVOKABLE
#define Q_SLOTS
#define Q_PRIVATE_SLOT(d, signature)
#define Q_SIGNALS
view setModel(model)
[17] //! [18]
QSqlQueryModel * model
[16]
QList< QChar > characters
QLineEdit * lineEdit
scene addItem(form)
QGraphicsItem * item
QFrame frame
[0]
lineEdit setCompleter(completer)
QCompleter * completer
[0]
QSizePolicy policy
QQuickView * view
[0]
QDBusArgument argument
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent