Qt 4.8
qcombobox.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QCOMBOBOX_H
43 #define QCOMBOBOX_H
44 
45 #include <QtGui/qwidget.h>
46 #include <QtGui/qabstractitemdelegate.h>
47 #include <QtCore/qabstractitemmodel.h>
48 #include <QtCore/qvariant.h>
49 
51 
53 
54 QT_MODULE(Gui)
55 #ifndef QT_NO_COMBOBOX
56 
57 class QAbstractItemView;
58 class QLineEdit;
59 class QComboBoxPrivate;
60 class QCompleter;
61 
63 {
64  Q_OBJECT
65 
68  bool editable;
69  int count;
70  QString currentText;
71  int currentIndex;
72  int maxVisibleItems;
73  int maxCount;
74  InsertPolicy insertPolicy;
75  SizeAdjustPolicy sizeAdjustPolicy;
76  int minimumContentsLength;
77  QSize iconSize;
78 
79 #ifndef QT_NO_COMPLETER
82 #endif // QT_NO_COMPLETER
83 
85  bool frame;
87 
88 public:
89  explicit QComboBox(QWidget *parent = 0);
90  ~QComboBox();
91 
92  int maxVisibleItems() const;
93  void setMaxVisibleItems(int maxItems);
94 
95  int count() const;
96  void setMaxCount(int max);
97  int maxCount() const;
98 
99 #ifndef QT_NO_COMPLETER
100  bool autoCompletion() const;
101  void setAutoCompletion(bool enable);
102 
103  Qt::CaseSensitivity autoCompletionCaseSensitivity() const;
104  void setAutoCompletionCaseSensitivity(Qt::CaseSensitivity sensitivity);
105 #endif
106 
107  bool duplicatesEnabled() const;
108  void setDuplicatesEnabled(bool enable);
109 
110  void setFrame(bool);
111  bool hasFrame() const;
112 
113  inline int findText(const QString &text,
114  Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
115  { return findData(text, Qt::DisplayRole, flags); }
116  int findData(const QVariant &data, int role = Qt::UserRole,
117  Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const;
118 
126  InsertAlphabetically
127 #if defined(QT3_SUPPORT) && !defined(Q_MOC_RUN)
128  ,
129  NoInsertion = NoInsert,
130  AtTop = InsertAtTop,
131  AtCurrent = InsertAtCurrent,
132  AtBottom = InsertAtBottom,
133  AfterCurrent = InsertAfterCurrent,
134  BeforeCurrent = InsertBeforeCurrent
135 #endif
136  };
137 #ifdef QT3_SUPPORT
138  typedef InsertPolicy Policy;
139 #endif
140 
141  InsertPolicy insertPolicy() const;
142  void setInsertPolicy(InsertPolicy policy);
143 
147  AdjustToMinimumContentsLength, // ### Qt 5: remove
148  AdjustToMinimumContentsLengthWithIcon
149  };
150 
151  SizeAdjustPolicy sizeAdjustPolicy() const;
152  void setSizeAdjustPolicy(SizeAdjustPolicy policy);
153  int minimumContentsLength() const;
154  void setMinimumContentsLength(int characters);
155  QSize iconSize() const;
156  void setIconSize(const QSize &size);
157 
158  bool isEditable() const;
159  void setEditable(bool editable);
160  void setLineEdit(QLineEdit *edit);
161  QLineEdit *lineEdit() const;
162 #ifndef QT_NO_VALIDATOR
163  void setValidator(const QValidator *v);
164  const QValidator *validator() const;
165 #endif
166 
167 #ifndef QT_NO_COMPLETER
168  void setCompleter(QCompleter *c);
169  QCompleter *completer() const;
170 #endif
171 
172  QAbstractItemDelegate *itemDelegate() const;
173  void setItemDelegate(QAbstractItemDelegate *delegate);
174 
175  QAbstractItemModel *model() const;
176  void setModel(QAbstractItemModel *model);
177 
178  QModelIndex rootModelIndex() const;
179  void setRootModelIndex(const QModelIndex &index);
180 
181  int modelColumn() const;
182  void setModelColumn(int visibleColumn);
183 
184  int currentIndex() const;
185 
186  QString currentText() const;
187 
188  QString itemText(int index) const;
189  QIcon itemIcon(int index) const;
190  QVariant itemData(int index, int role = Qt::UserRole) const;
191 
192  inline void addItem(const QString &text, const QVariant &userData = QVariant());
193  inline void addItem(const QIcon &icon, const QString &text,
194  const QVariant &userData = QVariant());
195  inline void addItems(const QStringList &texts)
196  { insertItems(count(), texts); }
197 
198  inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
199  void insertItem(int index, const QIcon &icon, const QString &text,
200  const QVariant &userData = QVariant());
201  void insertItems(int index, const QStringList &texts);
202  void insertSeparator(int index);
203 
204  void removeItem(int index);
205 
206  void setItemText(int index, const QString &text);
207  void setItemIcon(int index, const QIcon &icon);
208  void setItemData(int index, const QVariant &value, int role = Qt::UserRole);
209 
210  QAbstractItemView *view() const;
211  void setView(QAbstractItemView *itemView);
212 
213  QSize sizeHint() const;
214  QSize minimumSizeHint() const;
215 
216  virtual void showPopup();
217  virtual void hidePopup();
218 
219  bool event(QEvent *event);
220 
221 public Q_SLOTS:
222  void clear();
223  void clearEditText();
224  void setEditText(const QString &text);
225  void setCurrentIndex(int index);
226 
227 Q_SIGNALS:
228  void editTextChanged(const QString &);
229  void activated(int index);
230  void activated(const QString &);
231  void highlighted(int index);
232  void highlighted(const QString &);
233  void currentIndexChanged(int index);
234  void currentIndexChanged(const QString &);
235 
236 protected:
237  void focusInEvent(QFocusEvent *e);
238  void focusOutEvent(QFocusEvent *e);
239  void changeEvent(QEvent *e);
240  void resizeEvent(QResizeEvent *e);
241  void paintEvent(QPaintEvent *e);
242  void showEvent(QShowEvent *e);
243  void hideEvent(QHideEvent *e);
244  void mousePressEvent(QMouseEvent *e);
246  void keyPressEvent(QKeyEvent *e);
247  void keyReleaseEvent(QKeyEvent *e);
248 #ifndef QT_NO_WHEELEVENT
249  void wheelEvent(QWheelEvent *e);
250 #endif
254  void initStyleOption(QStyleOptionComboBox *option) const;
255 
256 public:
257 #ifdef QT3_SUPPORT
258  QT3_SUPPORT_CONSTRUCTOR QComboBox(QWidget *parent, const char *name);
259  QT3_SUPPORT_CONSTRUCTOR QComboBox(bool rw, QWidget *parent, const char *name = 0);
260  inline QT3_SUPPORT int currentItem() const { return currentIndex(); }
261  inline QT3_SUPPORT void setCurrentItem(int index) { setCurrentIndex(index); }
262  inline QT3_SUPPORT InsertPolicy insertionPolicy() const { return insertPolicy(); }
263  inline QT3_SUPPORT void setInsertionPolicy(InsertPolicy policy) { setInsertPolicy(policy); }
264  inline QT3_SUPPORT bool editable() const { return isEditable(); }
265  inline QT3_SUPPORT void popup() { showPopup(); }
266  inline QT3_SUPPORT void setCurrentText(const QString& text) {
267  int i = findText(text);
268  if (i != -1)
269  setCurrentIndex(i);
270  else if (isEditable())
271  setEditText(text);
272  else
273  setItemText(currentIndex(), text);
274  }
275  inline QT3_SUPPORT QString text(int index) const { return itemText(index); }
276 
277  inline QT3_SUPPORT QPixmap pixmap(int index) const
278  { return itemIcon(index).pixmap(iconSize(), isEnabled() ? QIcon::Normal : QIcon::Disabled); }
279  inline QT3_SUPPORT void insertStringList(const QStringList &list, int index = -1)
280  { insertItems((index < 0 ? count() : index), list); }
281  inline QT3_SUPPORT void insertItem(const QString &text, int index = -1)
282  { insertItem((index < 0 ? count() : index), text); }
283  inline QT3_SUPPORT void insertItem(const QPixmap &pix, int index = -1)
284  { insertItem((index < 0 ? count() : index), QIcon(pix), QString()); }
285  inline QT3_SUPPORT void insertItem(const QPixmap &pix, const QString &text, int index = -1)
286  { insertItem((index < 0 ? count() : index), QIcon(pix), text); }
287  inline QT3_SUPPORT void changeItem(const QString &text, int index)
288  { setItemText(index, text); }
289  inline QT3_SUPPORT void changeItem(const QPixmap &pix, int index)
290  { setItemIcon(index, QIcon(pix)); }
291  inline QT3_SUPPORT void changeItem(const QPixmap &pix, const QString &text, int index)
292  { setItemIcon(index, QIcon(pix)); setItemText(index, text); }
293  inline QT3_SUPPORT void clearValidator() { setValidator(0); }
294  inline QT3_SUPPORT void clearEdit() { clearEditText(); }
295 
296 Q_SIGNALS:
297  QT_MOC_COMPAT void textChanged(const QString &);
298 #endif
299 
300 protected:
301  QComboBox(QComboBoxPrivate &, QWidget *);
302 
303 private:
304  Q_DECLARE_PRIVATE(QComboBox)
305  Q_DISABLE_COPY(QComboBox)
306  Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item))
307  Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &))
308  Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index))
309  Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
310  Q_PRIVATE_SLOT(d_func(), void _q_returnPressed())
311  Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
312  Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &))
313  Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange())
314  Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end))
315  Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end))
316  Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
317  Q_PRIVATE_SLOT(d_func(), void _q_modelReset())
318 #ifdef QT_KEYPAD_NAVIGATION
319  Q_PRIVATE_SLOT(d_func(), void _q_completerActivated())
320 #endif
321 };
322 
323 inline void QComboBox::addItem(const QString &atext, const QVariant &auserData)
324 { insertItem(count(), atext, auserData); }
325 inline void QComboBox::addItem(const QIcon &aicon, const QString &atext,
326  const QVariant &auserData)
327 { insertItem(count(), aicon, atext, auserData); }
328 
329 inline void QComboBox::insertItem(int aindex, const QString &atext,
330  const QVariant &auserData)
331 { insertItem(aindex, QIcon(), atext, auserData); }
332 
333 #endif // QT_NO_COMBOBOX
334 
336 
338 
339 #endif // QCOMBOBOX_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
#define QT_MOC_COMPAT
Definition: qglobal.h:1128
The QAbstractItemDelegate class is used to display and edit data items from a model.
The QKeyEvent class describes a key event.
Definition: qevent.h:224
virtual void showEvent(QShowEvent *)
This event handler can be reimplemented in a subclass to receive widget show events which are passed ...
Definition: qwidget.cpp:9842
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
virtual void inputMethodEvent(QInputMethodEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive Input Method compo...
Definition: qwidget.cpp:9668
#define QT_MODULE(x)
Definition: qglobal.h:2783
virtual QSize minimumSizeHint() const
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
The QWheelEvent class contains parameters that describe a wheel event.
Definition: qevent.h:139
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition: qevent.h:396
virtual void mouseReleaseEvent(QMouseEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
Definition: qwidget.cpp:9286
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
virtual void mousePressEvent(QMouseEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
Definition: qwidget.cpp:9261
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
SizeAdjustPolicy
This enum specifies how the size hint of the QComboBox should adjust when new content is added or con...
Definition: qcombobox.h:144
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
The QCompleter class provides completions based on an item model.
Definition: qcompleter.h:64
virtual void resizeEvent(QResizeEvent *)
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
Definition: qwidget.cpp:9587
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
virtual void keyReleaseEvent(QKeyEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive key release events...
Definition: qwidget.cpp:9407
#define Q_SLOTS
Definition: qobjectdefs.h:71
virtual QVariant inputMethodQuery(Qt::InputMethodQuery) const
This method is only relevant for input widgets.
Definition: qwidget.cpp:9683
The QString class provides a Unicode character string.
Definition: qstring.h:83
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:323
#define Q_SIGNALS
Definition: qobjectdefs.h:72
InsertPolicy
This enum specifies what the QComboBox should do when a new string is entered by the user...
Definition: qcombobox.h:119
bool autoCompletion
whether the combobox provides auto-completion for editable items
Definition: qcombobox.h:80
virtual void focusOutEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
Definition: qwidget.cpp:9457
virtual QSize sizeHint() const
#define Q_ENUMS(x)
Definition: qobjectdefs.h:84
virtual void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events passed in event...
Definition: qwidget.cpp:9548
void addItems(const QStringList &texts)
Adds each of the strings in the given texts to the combobox.
Definition: qcombobox.h:195
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QValidator class provides validation of input text.
Definition: qvalidator.h:60
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:329
int modelColumn
the column in the model that is visible.
Definition: qcombobox.h:86
const char * name
Qt::CaseSensitivity autoCompletionCaseSensitivity
whether string comparisons are case-sensitive or case-insensitive for auto-completion ...
Definition: qcombobox.h:81
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
The QHideEvent class provides an event which is sent after a widget is hidden.
Definition: qevent.h:388
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
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:113
The QComboBox widget is a combined button and popup list.
Definition: qcombobox.h:62
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
static const char * data(const QByteArray &arr)
The QShowEvent class provides an event that is sent when a widget is shown.
Definition: qevent.h:380
virtual void keyPressEvent(QKeyEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
Definition: qwidget.cpp:9375
#define Q_OBJECT
Definition: qobjectdefs.h:157
bool isEnabled() const
Definition: qwidget.h:948
The QAbstractItemModel class provides the abstract interface for item model classes.
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
bool frame
whether the combo box draws itself with a frame
Definition: qcombobox.h:85
virtual void contextMenuEvent(QContextMenuEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive widget context men...
Definition: qwidget.cpp:9645
InputMethodQuery
Definition: qnamespace.h:1541
The QInputMethodEvent class provides parameters for input method events.
Definition: qevent.h:431
CaseSensitivity
Definition: qnamespace.h:1451
The QAbstractItemView class provides the basic functionality for item view classes.
virtual void wheelEvent(QWheelEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive wheel events for t...
Definition: qwidget.cpp:9326
virtual void hideEvent(QHideEvent *)
This event handler can be reimplemented in a subclass to receive widget hide events.
Definition: qwidget.cpp:9864
The QModelIndex class is used to locate data in a data model.
The QStyleOptionComboBox class is used to describe the parameter for drawing a combobox.
Definition: qstyleoption.h:796
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:66
quint16 index
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
bool duplicatesEnabled
whether the user can enter duplicate items into the combobox
Definition: qcombobox.h:84
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
static const KeyPair *const end
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define QT_END_HEADER
Definition: qglobal.h:137
virtual void focusInEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
Definition: qwidget.cpp:9431
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
#define text
Definition: qobjectdefs.h:80
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60