1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QCOMBOBOX_H
41#define QCOMBOBOX_H
42
43#include <QtWidgets/qtwidgetsglobal.h>
44#include <QtWidgets/qwidget.h>
45#include <QtWidgets/qabstractitemdelegate.h>
46#include <QtCore/qabstractitemmodel.h>
47#include <QtCore/qvariant.h>
48#include <QtGui/qvalidator.h>
49
50QT_REQUIRE_CONFIG(combobox);
51
52QT_BEGIN_NAMESPACE
53
54class QAbstractItemView;
55class QLineEdit;
56class QComboBoxPrivate;
57class QCompleter;
58
59class Q_WIDGETS_EXPORT QComboBox : public QWidget
60{
61 Q_OBJECT
62
63 Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
64 Q_PROPERTY(int count READ count)
65 Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText NOTIFY currentTextChanged USER true)
66 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
67 Q_PROPERTY(QVariant currentData READ currentData)
68 Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems)
69 Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount)
70 Q_PROPERTY(InsertPolicy insertPolicy READ insertPolicy WRITE setInsertPolicy)
71 Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy)
72 Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
73 Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
74
75#if QT_CONFIG(completer)
76 Q_PROPERTY(bool autoCompletion READ autoCompletion WRITE setAutoCompletion DESIGNABLE false)
77 Q_PROPERTY(Qt::CaseSensitivity autoCompletionCaseSensitivity READ autoCompletionCaseSensitivity WRITE setAutoCompletionCaseSensitivity DESIGNABLE false)
78#endif // QT_CONFIG(completer)
79
80 Q_PROPERTY(bool duplicatesEnabled READ duplicatesEnabled WRITE setDuplicatesEnabled)
81 Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
82 Q_PROPERTY(int modelColumn READ modelColumn WRITE setModelColumn)
83
84public:
85 explicit QComboBox(QWidget *parent = nullptr);
86 ~QComboBox();
87
88 int maxVisibleItems() const;
89 void setMaxVisibleItems(int maxItems);
90
91 int count() const;
92 void setMaxCount(int max);
93 int maxCount() const;
94
95#if QT_CONFIG(completer)
96 bool autoCompletion() const;
97 void setAutoCompletion(bool enable);
98
99 Qt::CaseSensitivity autoCompletionCaseSensitivity() const;
100 void setAutoCompletionCaseSensitivity(Qt::CaseSensitivity sensitivity);
101#endif
102
103 bool duplicatesEnabled() const;
104 void setDuplicatesEnabled(bool enable);
105
106 void setFrame(bool);
107 bool hasFrame() const;
108
109 inline int findText(const QString &text,
110 Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
111 { return findData(text, Qt::DisplayRole, flags); }
112 int findData(const QVariant &data, int role = Qt::UserRole,
113 Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const;
114
115 enum InsertPolicy {
116 NoInsert,
117 InsertAtTop,
118 InsertAtCurrent,
119 InsertAtBottom,
120 InsertAfterCurrent,
121 InsertBeforeCurrent,
122 InsertAlphabetically
123 };
124 Q_ENUM(InsertPolicy)
125
126 InsertPolicy insertPolicy() const;
127 void setInsertPolicy(InsertPolicy policy);
128
129 enum SizeAdjustPolicy {
130 AdjustToContents,
131 AdjustToContentsOnFirstShow,
132 AdjustToMinimumContentsLength, // ### Qt 6: remove
133 AdjustToMinimumContentsLengthWithIcon
134 };
135 Q_ENUM(SizeAdjustPolicy)
136
137 SizeAdjustPolicy sizeAdjustPolicy() const;
138 void setSizeAdjustPolicy(SizeAdjustPolicy policy);
139 int minimumContentsLength() const;
140 void setMinimumContentsLength(int characters);
141 QSize iconSize() const;
142 void setIconSize(const QSize &size);
143
144 bool isEditable() const;
145 void setEditable(bool editable);
146 void setLineEdit(QLineEdit *edit);
147 QLineEdit *lineEdit() const;
148#ifndef QT_NO_VALIDATOR
149 void setValidator(const QValidator *v);
150 const QValidator *validator() const;
151#endif
152
153#if QT_CONFIG(completer)
154 void setCompleter(QCompleter *c);
155 QCompleter *completer() const;
156#endif
157
158 QAbstractItemDelegate *itemDelegate() const;
159 void setItemDelegate(QAbstractItemDelegate *delegate);
160
161 QAbstractItemModel *model() const;
162 void setModel(QAbstractItemModel *model);
163
164 QModelIndex rootModelIndex() const;
165 void setRootModelIndex(const QModelIndex &index);
166
167 int modelColumn() const;
168 void setModelColumn(int visibleColumn);
169
170 int currentIndex() const;
171 QString currentText() const;
172 QVariant currentData(int role = Qt::UserRole) const;
173
174 QString itemText(int index) const;
175 QIcon itemIcon(int index) const;
176 QVariant itemData(int index, int role = Qt::UserRole) const;
177
178 inline void addItem(const QString &text, const QVariant &userData = QVariant());
179 inline void addItem(const QIcon &icon, const QString &text,
180 const QVariant &userData = QVariant());
181 inline void addItems(const QStringList &texts)
182 { insertItems(count(), texts); }
183
184 inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
185 void insertItem(int index, const QIcon &icon, const QString &text,
186 const QVariant &userData = QVariant());
187 void insertItems(int index, const QStringList &texts);
188 void insertSeparator(int index);
189
190 void removeItem(int index);
191
192 void setItemText(int index, const QString &text);
193 void setItemIcon(int index, const QIcon &icon);
194 void setItemData(int index, const QVariant &value, int role = Qt::UserRole);
195
196 QAbstractItemView *view() const;
197 void setView(QAbstractItemView *itemView);
198
199 QSize sizeHint() const override;
200 QSize minimumSizeHint() const override;
201
202 virtual void showPopup();
203 virtual void hidePopup();
204
205 bool event(QEvent *event) override;
206 QVariant inputMethodQuery(Qt::InputMethodQuery) const override;
207 Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, const QVariant &argument) const;
208
209public Q_SLOTS:
210 void clear();
211 void clearEditText();
212 void setEditText(const QString &text);
213 void setCurrentIndex(int index);
214 void setCurrentText(const QString &text);
215
216Q_SIGNALS:
217 void editTextChanged(const QString &);
218 void activated(int index);
219 void activated(const QString &);
220 void highlighted(int index);
221 void highlighted(const QString &);
222 void currentIndexChanged(int index);
223 void currentIndexChanged(const QString &);
224 void currentTextChanged(const QString &);
225
226protected:
227 void focusInEvent(QFocusEvent *e) override;
228 void focusOutEvent(QFocusEvent *e) override;
229 void changeEvent(QEvent *e) override;
230 void resizeEvent(QResizeEvent *e) override;
231 void paintEvent(QPaintEvent *e) override;
232 void showEvent(QShowEvent *e) override;
233 void hideEvent(QHideEvent *e) override;
234 void mousePressEvent(QMouseEvent *e) override;
235 void mouseReleaseEvent(QMouseEvent *e) override;
236 void keyPressEvent(QKeyEvent *e) override;
237 void keyReleaseEvent(QKeyEvent *e) override;
238#if QT_CONFIG(wheelevent)
239 void wheelEvent(QWheelEvent *e) override;
240#endif
241#ifndef QT_NO_CONTEXTMENU
242 void contextMenuEvent(QContextMenuEvent *e) override;
243#endif // QT_NO_CONTEXTMENU
244 void inputMethodEvent(QInputMethodEvent *) override;
245 void initStyleOption(QStyleOptionComboBox *option) const;
246
247
248protected:
249 QComboBox(QComboBoxPrivate &, QWidget *);
250
251private:
252 Q_DECLARE_PRIVATE(QComboBox)
253 Q_DISABLE_COPY(QComboBox)
254 Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item))
255 Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &))
256 Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index))
257 Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
258 Q_PRIVATE_SLOT(d_func(), void _q_returnPressed())
259 Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
260 Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &))
261 Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange())
262 Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end))
263 Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end))
264 Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
265 Q_PRIVATE_SLOT(d_func(), void _q_modelReset())
266#if QT_CONFIG(completer)
267 Q_PRIVATE_SLOT(d_func(), void _q_completerActivated(const QModelIndex &index))
268#endif
269};
270
271inline void QComboBox::addItem(const QString &atext, const QVariant &auserData)
272{ insertItem(count(), atext, auserData); }
273inline void QComboBox::addItem(const QIcon &aicon, const QString &atext,
274 const QVariant &auserData)
275{ insertItem(count(), aicon, atext, auserData); }
276
277inline void QComboBox::insertItem(int aindex, const QString &atext,
278 const QVariant &auserData)
279{ insertItem(aindex, QIcon(), atext, auserData); }
280
281QT_END_NAMESPACE
282
283#endif // QCOMBOBOX_H
284