qcombobox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #ifndef QCOMBOBOX_H
  40. #define QCOMBOBOX_H
  41. #include <QtWidgets/qwidget.h>
  42. #include <QtWidgets/qabstractitemdelegate.h>
  43. #include <QtCore/qabstractitemmodel.h>
  44. #include <QtCore/qvariant.h>
  45. QT_BEGIN_NAMESPACE
  46. #ifndef QT_NO_COMBOBOX
  47. class QAbstractItemView;
  48. class QLineEdit;
  49. class QComboBoxPrivate;
  50. class QCompleter;
  51. class Q_WIDGETS_EXPORT QComboBox : public QWidget
  52. {
  53. Q_OBJECT
  54. Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
  55. Q_PROPERTY(int count READ count)
  56. Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText NOTIFY currentTextChanged USER true)
  57. Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
  58. Q_PROPERTY(QVariant currentData READ currentData)
  59. Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems)
  60. Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount)
  61. Q_PROPERTY(InsertPolicy insertPolicy READ insertPolicy WRITE setInsertPolicy)
  62. Q_PROPERTY(SizeAdjustPolicy sizeAdjustPolicy READ sizeAdjustPolicy WRITE setSizeAdjustPolicy)
  63. Q_PROPERTY(int minimumContentsLength READ minimumContentsLength WRITE setMinimumContentsLength)
  64. Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize)
  65. #ifndef QT_NO_COMPLETER
  66. Q_PROPERTY(bool autoCompletion READ autoCompletion WRITE setAutoCompletion DESIGNABLE false)
  67. Q_PROPERTY(Qt::CaseSensitivity autoCompletionCaseSensitivity READ autoCompletionCaseSensitivity WRITE setAutoCompletionCaseSensitivity DESIGNABLE false)
  68. #endif // QT_NO_COMPLETER
  69. Q_PROPERTY(bool duplicatesEnabled READ duplicatesEnabled WRITE setDuplicatesEnabled)
  70. Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
  71. Q_PROPERTY(int modelColumn READ modelColumn WRITE setModelColumn)
  72. public:
  73. explicit QComboBox(QWidget *parent = Q_NULLPTR);
  74. ~QComboBox();
  75. int maxVisibleItems() const;
  76. void setMaxVisibleItems(int maxItems);
  77. int count() const;
  78. void setMaxCount(int max);
  79. int maxCount() const;
  80. #ifndef QT_NO_COMPLETER
  81. bool autoCompletion() const;
  82. void setAutoCompletion(bool enable);
  83. Qt::CaseSensitivity autoCompletionCaseSensitivity() const;
  84. void setAutoCompletionCaseSensitivity(Qt::CaseSensitivity sensitivity);
  85. #endif
  86. bool duplicatesEnabled() const;
  87. void setDuplicatesEnabled(bool enable);
  88. void setFrame(bool);
  89. bool hasFrame() const;
  90. inline int findText(const QString &text,
  91. Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const
  92. { return findData(text, Qt::DisplayRole, flags); }
  93. int findData(const QVariant &data, int role = Qt::UserRole,
  94. Qt::MatchFlags flags = static_cast<Qt::MatchFlags>(Qt::MatchExactly|Qt::MatchCaseSensitive)) const;
  95. enum InsertPolicy {
  96. NoInsert,
  97. InsertAtTop,
  98. InsertAtCurrent,
  99. InsertAtBottom,
  100. InsertAfterCurrent,
  101. InsertBeforeCurrent,
  102. InsertAlphabetically
  103. };
  104. Q_ENUM(InsertPolicy)
  105. InsertPolicy insertPolicy() const;
  106. void setInsertPolicy(InsertPolicy policy);
  107. enum SizeAdjustPolicy {
  108. AdjustToContents,
  109. AdjustToContentsOnFirstShow,
  110. AdjustToMinimumContentsLength, // ### Qt 6: remove
  111. AdjustToMinimumContentsLengthWithIcon
  112. };
  113. Q_ENUM(SizeAdjustPolicy)
  114. SizeAdjustPolicy sizeAdjustPolicy() const;
  115. void setSizeAdjustPolicy(SizeAdjustPolicy policy);
  116. int minimumContentsLength() const;
  117. void setMinimumContentsLength(int characters);
  118. QSize iconSize() const;
  119. void setIconSize(const QSize &size);
  120. bool isEditable() const;
  121. void setEditable(bool editable);
  122. void setLineEdit(QLineEdit *edit);
  123. QLineEdit *lineEdit() const;
  124. #ifndef QT_NO_VALIDATOR
  125. void setValidator(const QValidator *v);
  126. const QValidator *validator() const;
  127. #endif
  128. #ifndef QT_NO_COMPLETER
  129. void setCompleter(QCompleter *c);
  130. QCompleter *completer() const;
  131. #endif
  132. QAbstractItemDelegate *itemDelegate() const;
  133. void setItemDelegate(QAbstractItemDelegate *delegate);
  134. QAbstractItemModel *model() const;
  135. void setModel(QAbstractItemModel *model);
  136. QModelIndex rootModelIndex() const;
  137. void setRootModelIndex(const QModelIndex &index);
  138. int modelColumn() const;
  139. void setModelColumn(int visibleColumn);
  140. int currentIndex() const;
  141. QString currentText() const;
  142. QVariant currentData(int role = Qt::UserRole) const;
  143. QString itemText(int index) const;
  144. QIcon itemIcon(int index) const;
  145. QVariant itemData(int index, int role = Qt::UserRole) const;
  146. inline void addItem(const QString &text, const QVariant &userData = QVariant());
  147. inline void addItem(const QIcon &icon, const QString &text,
  148. const QVariant &userData = QVariant());
  149. inline void addItems(const QStringList &texts)
  150. { insertItems(count(), texts); }
  151. inline void insertItem(int index, const QString &text, const QVariant &userData = QVariant());
  152. void insertItem(int index, const QIcon &icon, const QString &text,
  153. const QVariant &userData = QVariant());
  154. void insertItems(int index, const QStringList &texts);
  155. void insertSeparator(int index);
  156. void removeItem(int index);
  157. void setItemText(int index, const QString &text);
  158. void setItemIcon(int index, const QIcon &icon);
  159. void setItemData(int index, const QVariant &value, int role = Qt::UserRole);
  160. QAbstractItemView *view() const;
  161. void setView(QAbstractItemView *itemView);
  162. QSize sizeHint() const Q_DECL_OVERRIDE;
  163. QSize minimumSizeHint() const Q_DECL_OVERRIDE;
  164. virtual void showPopup();
  165. virtual void hidePopup();
  166. bool event(QEvent *event) Q_DECL_OVERRIDE;
  167. QVariant inputMethodQuery(Qt::InputMethodQuery) const Q_DECL_OVERRIDE;
  168. Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, const QVariant &argument) const;
  169. public Q_SLOTS:
  170. void clear();
  171. void clearEditText();
  172. void setEditText(const QString &text);
  173. void setCurrentIndex(int index);
  174. void setCurrentText(const QString &text);
  175. Q_SIGNALS:
  176. void editTextChanged(const QString &);
  177. void activated(int index);
  178. void activated(const QString &);
  179. void highlighted(int index);
  180. void highlighted(const QString &);
  181. void currentIndexChanged(int index);
  182. void currentIndexChanged(const QString &);
  183. void currentTextChanged(const QString &);
  184. protected:
  185. void focusInEvent(QFocusEvent *e) Q_DECL_OVERRIDE;
  186. void focusOutEvent(QFocusEvent *e) Q_DECL_OVERRIDE;
  187. void changeEvent(QEvent *e) Q_DECL_OVERRIDE;
  188. void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
  189. void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
  190. void showEvent(QShowEvent *e) Q_DECL_OVERRIDE;
  191. void hideEvent(QHideEvent *e) Q_DECL_OVERRIDE;
  192. void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
  193. void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
  194. void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
  195. void keyReleaseEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
  196. #ifndef QT_NO_WHEELEVENT
  197. void wheelEvent(QWheelEvent *e) Q_DECL_OVERRIDE;
  198. #endif
  199. #ifndef QT_NO_CONTEXTMENU
  200. void contextMenuEvent(QContextMenuEvent *e) Q_DECL_OVERRIDE;
  201. #endif // QT_NO_CONTEXTMENU
  202. void inputMethodEvent(QInputMethodEvent *) Q_DECL_OVERRIDE;
  203. void initStyleOption(QStyleOptionComboBox *option) const;
  204. protected:
  205. QComboBox(QComboBoxPrivate &, QWidget *);
  206. private:
  207. Q_DECLARE_PRIVATE(QComboBox)
  208. Q_DISABLE_COPY(QComboBox)
  209. Q_PRIVATE_SLOT(d_func(), void _q_itemSelected(const QModelIndex &item))
  210. Q_PRIVATE_SLOT(d_func(), void _q_emitHighlighted(const QModelIndex &))
  211. Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentIndexChanged(const QModelIndex &index))
  212. Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
  213. Q_PRIVATE_SLOT(d_func(), void _q_returnPressed())
  214. Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
  215. Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &))
  216. Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange())
  217. Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end))
  218. Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end))
  219. Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
  220. Q_PRIVATE_SLOT(d_func(), void _q_modelReset())
  221. #ifndef QT_NO_COMPLETER
  222. Q_PRIVATE_SLOT(d_func(), void _q_completerActivated(const QModelIndex &index))
  223. #endif
  224. };
  225. inline void QComboBox::addItem(const QString &atext, const QVariant &auserData)
  226. { insertItem(count(), atext, auserData); }
  227. inline void QComboBox::addItem(const QIcon &aicon, const QString &atext,
  228. const QVariant &auserData)
  229. { insertItem(count(), aicon, atext, auserData); }
  230. inline void QComboBox::insertItem(int aindex, const QString &atext,
  231. const QVariant &auserData)
  232. { insertItem(aindex, QIcon(), atext, auserData); }
  233. #endif // QT_NO_COMBOBOX
  234. QT_END_NAMESPACE
  235. #endif // QCOMBOBOX_H