Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qcombobox_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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_P_H
5#define QCOMBOBOX_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtWidgets/private/qtwidgetsglobal_p.h>
19#include "QtWidgets/qcombobox.h"
20
21#include "QtWidgets/qabstractslider.h"
22#include "QtWidgets/qapplication.h"
23#include "QtWidgets/qstyleditemdelegate.h"
24#include "QtGui/qstandarditemmodel.h"
25#include "QtWidgets/qlineedit.h"
26#include "QtWidgets/qlistview.h"
27#include "QtGui/qpainter.h"
28#include "QtWidgets/qstyle.h"
29#include "QtWidgets/qstyleoption.h"
30#include "QtCore/qpair.h"
31#include "QtCore/qtimer.h"
32#include "private/qwidget_p.h"
33#include "QtCore/qpointer.h"
34#if QT_CONFIG(completer)
35#include "QtWidgets/qcompleter.h"
36#endif
37#include "QtGui/qevent.h"
38#include "QtCore/qdebug.h"
39
40#include <limits.h>
41
43
45
46class QPlatformMenu;
47
49{
51public:
52 QComboBoxListView(QComboBox *cmb = nullptr) : combo(cmb)
53 {
54 if (cmb)
55 setScreen(cmb->screen());
56 }
57
58protected:
60 {
63 }
64
65 void initViewItemOption(QStyleOptionViewItem *option) const override
66 {
68 option->showDecorationSelected = true;
69 if (combo)
70 option->font = combo->font();
71 }
72
73 void paintEvent(QPaintEvent *e) override
74 {
75 if (combo) {
77 opt.initFrom(combo);
78 opt.editable = combo->isEditable();
79 if (combo->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt, combo)) {
80 //we paint the empty menu area to avoid having blank space that can happen when scrolling
82 menuOpt.initFrom(this);
83 menuOpt.palette = palette();
84 menuOpt.state = QStyle::State_None;
86 menuOpt.menuRect = e->rect();
87 menuOpt.maxIconWidth = 0;
88 menuOpt.reservedShortcutWidth = 0;
90 combo->style()->drawControl(QStyle::CE_MenuEmptyArea, &menuOpt, &p, this);
91 }
92 }
94 }
95
96private:
97 QComboBox *combo;
98};
99
101{
103
104public:
106 : QWidget(parent), sliderAction(action)
107 {
110 }
111 QSize sizeHint() const override {
112 return QSize(20, style()->pixelMetric(QStyle::PM_MenuScrollerHeight, nullptr, this));
113 }
114
115protected:
116 inline void stopTimer() {
117 timer.stop();
118 }
119
120 inline void startTimer() {
121 timer.start(100, this);
122 fast = false;
123 }
124
125 void enterEvent(QEnterEvent *) override {
126 startTimer();
127 }
128
129 void leaveEvent(QEvent *) override {
130 stopTimer();
131 }
132 void timerEvent(QTimerEvent *e) override {
133 if (e->timerId() == timer.timerId()) {
134 emit doScroll(sliderAction);
135 if (fast) {
136 emit doScroll(sliderAction);
137 emit doScroll(sliderAction);
138 }
139 }
140 }
141 void hideEvent(QHideEvent *) override {
142 stopTimer();
143 }
144
146 {
147 // Enable fast scrolling if the cursor is directly above or below the popup.
148 const int mouseX = e->position().toPoint().x();
149 const int mouseY = e->position().toPoint().y();
150 const bool horizontallyInside = pos().x() < mouseX && mouseX < rect().right() + 1;
151 const bool verticallyOutside = (sliderAction == QAbstractSlider::SliderSingleStepAdd) ?
152 rect().bottom() + 1 < mouseY : mouseY < pos().y();
153
154 fast = horizontallyInside && verticallyOutside;
155 }
156
157 void paintEvent(QPaintEvent *) override {
158 QPainter p(this);
159 QStyleOptionMenuItem menuOpt;
160 menuOpt.initFrom(this);
162 menuOpt.menuRect = rect();
163 menuOpt.maxIconWidth = 0;
164 menuOpt.reservedShortcutWidth = 0;
166 if (sliderAction == QAbstractSlider::SliderSingleStepAdd)
168 p.eraseRect(rect());
170 }
171
173 void doScroll(int action);
174
175private:
178 bool fast = false;
179};
180
181class Q_WIDGETS_EXPORT QComboBoxPrivateContainer : public QFrame
182{
184
185public:
187 QAbstractItemView *itemView() const;
188 void setItemView(QAbstractItemView *itemView);
189 int spacing() const;
190 int topMargin() const;
191 int bottomMargin() const { return topMargin(); }
192 void updateTopBottomMargin();
193 void updateStyleSettings();
194
198
199public Q_SLOTS:
200 void scrollItemView(int action);
201 void hideScrollers();
202 void updateScrollers();
203 void viewDestroyed();
204
205protected:
206 void changeEvent(QEvent *e) override;
207 bool eventFilter(QObject *o, QEvent *e) override;
208 void mousePressEvent(QMouseEvent *e) override;
209 void mouseReleaseEvent(QMouseEvent *e) override;
210 void showEvent(QShowEvent *e) override;
211 void hideEvent(QHideEvent *e) override;
212 void timerEvent(QTimerEvent *timerEvent) override;
213 void resizeEvent(QResizeEvent *e) override;
214 void paintEvent(QPaintEvent *e) override;
215 QStyleOptionComboBox comboStyleOption() const;
216
220
221private:
222 QComboBox *combo;
223 QAbstractItemView *view = nullptr;
224 QComboBoxPrivateScroller *top = nullptr;
226 QElapsedTimer popupTimer;
227 bool maybeIgnoreMouseButtonRelease = false;
228
229 friend class QComboBox;
230 friend class QComboBoxPrivate;
231};
232
234{
236public:
238 : QAbstractItemDelegate(parent), mCombo(cmb), pressedIndex(-1)
239 {}
240
241protected:
243 const QStyleOptionViewItem &option,
244 const QModelIndex &index) const override {
245 QStyleOptionMenuItem opt = getStyleOption(option, index);
247 mCombo->style()->drawControl(QStyle::CE_MenuItem, &opt, painter, mCombo);
248 }
249 QSize sizeHint(const QStyleOptionViewItem &option,
250 const QModelIndex &index) const override {
251 QStyleOptionMenuItem opt = getStyleOption(option, index);
252 return mCombo->style()->sizeFromContents(
253 QStyle::CT_MenuItem, &opt, option.rect.size(), mCombo);
254 }
255 bool editorEvent(QEvent *event, QAbstractItemModel *model,
256 const QStyleOptionViewItem &option, const QModelIndex &index) override;
257
258private:
259 QStyleOptionMenuItem getStyleOption(const QStyleOptionViewItem &option,
260 const QModelIndex &index) const;
261 QComboBox *mCombo;
262 int pressedIndex;
263};
264
266{
268public:
270 : QStyledItemDelegate(parent), mCombo(cmb)
271 {}
272
273 static bool isSeparator(const QModelIndex &index) {
274 return index.data(Qt::AccessibleDescriptionRole).toString()
275 == QLatin1StringView("separator");
276 }
279 if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(model))
280 if (QStandardItem *item = m->itemFromIndex(index))
282 }
283
284protected:
286 const QStyleOptionViewItem &option,
287 const QModelIndex &index) const override {
288 if (isSeparator(index)) {
289 QRect rect = option.rect;
290 if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(option.widget))
291 rect.setWidth(view->viewport()->width());
293 opt.rect = rect;
294 mCombo->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, mCombo);
295 } else {
297 }
298 }
299
300 QSize sizeHint(const QStyleOptionViewItem &option,
301 const QModelIndex &index) const override {
302 if (isSeparator(index)) {
303 int pm = mCombo->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, mCombo);
304 return QSize(pm, pm);
305 }
307 }
308private:
309 QComboBox *mCombo;
310};
311
313{
314 Q_DECLARE_PUBLIC(QComboBox)
315public:
318 void init();
319 QComboBoxPrivateContainer* viewContainer();
320 void updateLineEditGeometry();
321 Qt::MatchFlags matchFlags() const;
322 void _q_editingFinished();
323 void _q_returnPressed();
325 void _q_itemSelected(const QModelIndex &item);
326 bool contains(const QString &text, int role);
327 void emitActivated(const QModelIndex &index);
328 void _q_emitHighlighted(const QModelIndex &index);
329 void _q_emitCurrentIndexChanged(const QModelIndex &index);
330 void _q_modelDestroyed();
331 void _q_modelReset();
332#if QT_CONFIG(completer)
333 void _q_completerActivated(const QModelIndex &index);
334#endif
335 void _q_resetButton();
336 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
337 void _q_updateIndexBeforeChange();
338 void _q_rowsInserted(const QModelIndex &parent, int start, int end);
339 void _q_rowsRemoved(const QModelIndex &parent, int start, int end);
340 void updateArrow(QStyle::StateFlag state);
341 bool updateHoverControl(const QPoint &pos);
342 void trySetValidIndex();
343 QRect popupGeometry(const QPoint &globalPos) const;
344 QStyle::SubControl newHoverControl(const QPoint &pos);
345 int computeWidthHint() const;
346 QSize recomputeSizeHint(QSize &sh) const;
347 void adjustComboBoxSize();
348 QString itemText(const QModelIndex &index) const;
349 QIcon itemIcon(const QModelIndex &index) const;
350 int itemRole() const;
351 void updateLayoutDirection();
352 void setCurrentIndex(const QModelIndex &index);
353 void updateDelegate(bool force = false);
354 void keyboardSearchString(const QString &text);
355 void modelChanged();
356 void updateViewContainerPaletteAndOpacity();
357 void updateFocusPolicy();
358 void showPopupFromMouseEvent(QMouseEvent *e);
359 void doHidePopup();
360 void updateCurrentText(const QString &text);
361
362#ifdef Q_OS_MAC
363 void cleanupNativePopup();
364 bool showNativePopup();
365 struct IndexSetter {
366 int index;
367 QComboBox *cb;
368
369 void operator()(void)
370 {
371 cb->setCurrentIndex(index);
372 cb->d_func()->emitActivated(cb->d_func()->currentIndex);
373 }
374 };
375#endif
376
378 QLineEdit *lineEdit = nullptr;
379 QComboBoxPrivateContainer *container = nullptr;
380#ifdef Q_OS_MAC
381 QPlatformMenu *m_platformMenu = nullptr;
382#endif
395 int minimumContentsLength = 0;
396 int indexBeforeChange = -1;
397 int maxVisibleItems = 10;
398 int maxCount = (std::numeric_limits<int>::max)();
399 int modelColumn = 0;
400 int placeholderIndex = -1;
401 bool shownOnce : 1;
403 bool frame : 1;
404 bool inserting : 1;
405 bool hidingPopup : 1;
406};
407
409
410#endif // QCOMBOBOX_P_H
The QAbstractItemDelegate class is used to display and edit data items from a model.
virtual Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
The QAbstractItemView class provides the basic functionality for item view classes.
SliderAction
\value SliderNoAction \value SliderSingleStepAdd \value SliderSingleStepSub \value SliderPageStepAdd ...
\inmodule QtCore
Definition qbasictimer.h:18
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
static bool isSeparator(const QModelIndex &index)
QComboBoxDelegate(QObject *parent, QComboBox *cmb)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
static void setSeparator(QAbstractItemModel *model, const QModelIndex &index)
void resizeEvent(QResizeEvent *event) override
Definition qcombobox_p.h:59
QComboBoxListView(QComboBox *cmb=nullptr)
Definition qcombobox_p.h:52
void initViewItemOption(QStyleOptionViewItem *option) const override
Definition qcombobox_p.h:65
void paintEvent(QPaintEvent *e) override
Definition qcombobox_p.h:73
void itemSelected(const QModelIndex &)
void mouseMoveEvent(QMouseEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
void enterEvent(QEnterEvent *) override
This event handler can be reimplemented in a subclass to receive widget enter events which are passed...
void doScroll(int action)
void timerEvent(QTimerEvent *e) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
void leaveEvent(QEvent *) override
This event handler can be reimplemented in a subclass to receive widget leave events which are passed...
QComboBoxPrivateScroller(QAbstractSlider::SliderAction action, QWidget *parent)
void hideEvent(QHideEvent *) override
This event handler can be reimplemented in a subclass to receive widget hide events.
QSize sizeHint() const override
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
QPersistentModelIndex currentIndex
QPersistentModelIndex root
QString placeholderText
bool contains(const QString &text, int role)
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
@ 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
@ AdjustToContentsOnFirstShow
Definition qcombobox.h:83
bool isEditable() const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
QComboMenuDelegate(QObject *parent, QComboBox *cmb)
\inmodule QtCore
\inmodule QtGui
Definition qevent.h:164
\inmodule QtCore
Definition qcoreevent.h:45
The QFrame class is the base class of widgets that can have a frame.
Definition qframe.h:17
void paintEvent(QPaintEvent *) override
\reimp
Definition qframe.cpp:477
void changeEvent(QEvent *) override
\reimp
Definition qframe.cpp:498
void setFlags(GraphicsItemFlags flags)
Sets the item flags to flags.
GraphicsItemFlags flags() const
Returns this item's flags.
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 QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
The QListView class provides a list or icon view onto a model.
Definition qlistview.h:17
void resizeEvent(QResizeEvent *e) override
\reimp
void initViewItemOption(QStyleOptionViewItem *option) const override
\reimp
QSize contentsSize() const
void resizeContents(int width, int height)
void paintEvent(QPaintEvent *e) override
\reimp
\inmodule QtCore
\inmodule QtGui
Definition qevent.h:195
\inmodule QtCore
Definition qobject.h:90
int startTimer(int interval, Qt::TimerType timerType=Qt::CoarseTimer)
This is an overloaded function that will start a timer of type timerType and a timeout of interval mi...
Definition qobject.cpp:1792
virtual bool eventFilter(QObject *watched, QEvent *event)
Filters events if this object has been installed as an event filter for the watched object.
Definition qobject.cpp:1518
virtual void timerEvent(QTimerEvent *event)
This event handler can be reimplemented in a subclass to receive timer events for the object.
Definition qobject.cpp:1433
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:485
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
const QBrush & window() const
Returns the window (general background) brush of the current color group.
Definition qpalette.h:92
\inmodule QtCore\reentrant
Definition qpoint.h:23
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:127
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:132
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int bottom() const noexcept
Returns the y-coordinate of the rectangle's bottom edge.
Definition qrect.h:181
constexpr QSize size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:241
constexpr int right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:178
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
The QStandardItemModel class provides a generic model for storing custom data.
The QStandardItem class provides an item for use with the QStandardItemModel class.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
\variable QStyleOptionToolButton::features
\variable QStyleOptionProgressBar::minimum
MenuItemType menuItemType
The QStyleOption class stores the parameters used by QStyle functions.
QStyle::State state
QPalette palette
void initFrom(const QWidget *w)
StateFlag
This enum describes flags that are used when drawing primitive elements.
Definition qstyle.h:65
@ State_DownArrow
Definition qstyle.h:73
@ State_None
Definition qstyle.h:66
@ CT_MenuItem
Definition qstyle.h:552
@ SH_ComboBox_Popup
Definition qstyle.h:608
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=nullptr, const QWidget *widget=nullptr, QStyleHintReturn *returnData=nullptr) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
@ CE_MenuItem
Definition qstyle.h:190
@ CE_MenuEmptyArea
Definition qstyle.h:195
@ CE_MenuScroller
Definition qstyle.h:191
@ PM_DefaultFrameWidth
Definition qstyle.h:420
@ PM_MenuScrollerHeight
Definition qstyle.h:450
@ PE_IndicatorToolBarSeparator
Definition qstyle.h:142
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w=nullptr) const =0
Draws the given element with the provided painter with the style options specified by option.
SubControl
This enum describes the available sub controls.
Definition qstyle.h:347
@ SC_None
Definition qstyle.h:348
The QStyledItemDelegate class provides display and editing facilities for data items from a model.
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Returns the size needed by the delegate to display the item specified by index, taking into account t...
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Renders the delegate using the given painter and style option for the item specified by index.
\inmodule QtCore
Definition qcoreevent.h:359
\inmodule QtCore
Definition qtimer.h:20
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition qtimer.cpp:208
int timerId() const
Returns the ID of the timer if the timer is running; otherwise returns -1.
Definition qtimer.cpp:172
void stop()
Stops the timer.
Definition qtimer.cpp:226
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
virtual void hideEvent(QHideEvent *event)
This event handler can be reimplemented in a subclass to receive widget hide events.
void setSizePolicy(QSizePolicy)
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
QPoint pos
the position of the widget within its parent widget
Definition qwidget.h:111
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
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
QStyle * style() const
Definition qwidget.cpp:2607
QFont font
the font currently set for the widget
Definition qwidget.h:133
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 showEvent(QShowEvent *event)
This event handler can be reimplemented in a subclass to receive widget show events which are passed ...
int width
the width of the window's geometry
Definition qwindow.h:82
QString text
qreal spacing
double e
rect
[4]
QStyleOptionButton opt
else opt state
[0]
Combined button and popup list for selecting options.
@ WA_NoMousePropagation
Definition qnamespace.h:338
@ AccessibleDescriptionRole
@ ItemIsSelectable
@ ItemIsEnabled
static bool isSeparator(char c)
Definition qhsts.cpp:278
const GLfloat * m
GLint GLsizei GLsizei height
GLuint index
[2]
GLuint GLuint end
GLdouble GLdouble GLdouble GLdouble top
GLint GLsizei width
GLint GLint bottom
GLuint start
struct _cl_event * event
GLsizei maxCount
Definition qopenglext.h:677
GLfloat GLfloat p
[1]
GLuint GLenum option
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
#define Q_AUTOTEST_EXPORT
#define QT_REQUIRE_CONFIG(feature)
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
#define emit
QSqlQueryModel * model
[16]
QLineEdit * lineEdit
QTimer * timer
[3]
QGraphicsItem * item
view viewport() -> scroll(dx, dy, deviceRect)
QPainter painter(this)
[7]
QQuickView * view
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent