Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qslider.cpp
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#include "qslider.h"
5#if QT_CONFIG(accessibility)
6#include "qaccessible.h"
7#endif
8#include "qapplication.h"
9#include "qevent.h"
10#include "qpainter.h"
11#include "qstyle.h"
12#include "qstyleoption.h"
13#include "qstylepainter.h"
14#include "private/qapplication_p.h"
15#include "private/qabstractslider_p.h"
16#include "qdebug.h"
17
19
38
54
56{
57 Q_Q(QSlider);
58 QStyleOptionSlider opt;
59 // ### This is (also) reached from the ctor which is unfortunate since a possible
60 // ### re-implementation of initStyleOption is then not called.
61 q->initStyleOption(&opt);
63}
64
66{
67 Q_Q(const QSlider);
68 QStyleOptionSlider opt;
69 q->initStyleOption(&opt);
70 QRect gr = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, q);
71 QRect sr = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, q);
72 int sliderMin, sliderMax, sliderLength;
73
75 sliderLength = sr.width();
76 sliderMin = gr.x();
77 sliderMax = gr.right() - sliderLength + 1;
78 } else {
79 sliderLength = sr.height();
80 sliderMin = gr.y();
81 sliderMax = gr.bottom() - sliderLength + 1;
82 }
84 sliderMax - sliderMin, opt.upsideDown);
85}
86
87inline int QSliderPrivate::pick(const QPoint &pt) const
88{
89 return orientation == Qt::Horizontal ? pt.x() : pt.y();
90}
91
99void QSlider::initStyleOption(QStyleOptionSlider *option) const
100{
101 if (!option)
102 return;
103
104 Q_D(const QSlider);
105 option->initFrom(this);
106 option->subControls = QStyle::SC_None;
107 option->activeSubControls = QStyle::SC_None;
108 option->orientation = d->orientation;
109 option->maximum = d->maximum;
110 option->minimum = d->minimum;
111 option->tickPosition = (QSlider::TickPosition)d->tickPosition;
112 option->tickInterval = d->tickInterval;
113 option->upsideDown = (d->orientation == Qt::Horizontal) ?
114 (d->invertedAppearance != (option->direction == Qt::RightToLeft))
115 : (!d->invertedAppearance);
116 option->direction = Qt::LeftToRight; // we use the upsideDown option instead
117 option->sliderPosition = d->position;
118 option->sliderValue = d->value;
119 option->singleStep = d->singleStep;
120 option->pageStep = d->pageStep;
121 if (d->orientation == Qt::Horizontal)
123
124 if (d->pressedControl) {
125 option->activeSubControls = d->pressedControl;
127 } else {
128 option->activeSubControls = d->hoverControl;
129 }
130}
131
133{
134 Q_Q(QSlider);
135 QRect lastHoverRect = hoverRect;
136 QStyle::SubControl lastHoverControl = hoverControl;
137 bool doesHover = q->testAttribute(Qt::WA_Hover);
138 if (lastHoverControl != newHoverControl(pos) && doesHover) {
139 q->update(lastHoverRect);
140 q->update(hoverRect);
141 return true;
142 }
143 return !doesHover;
144}
145
147{
148 Q_Q(QSlider);
149 QStyleOptionSlider opt;
150 q->initStyleOption(&opt);
151 opt.subControls = QStyle::SC_All;
152 QRect handleRect = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, q);
153 QRect grooveRect = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, q);
154 QRect tickmarksRect = q->style()->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderTickmarks, q);
155
156 if (handleRect.contains(pos)) {
157 hoverRect = handleRect;
159 } else if (grooveRect.contains(pos)) {
160 hoverRect = grooveRect;
162 } else if (tickmarksRect.contains(pos)) {
163 hoverRect = tickmarksRect;
165 } else {
166 hoverRect = QRect();
168 }
169
170 return hoverControl;
171}
172
254 : QSlider(Qt::Vertical, parent)
255{
256}
257
265 : QAbstractSlider(*new QSliderPrivate, parent)
266{
267 d_func()->orientation = orientation;
268 d_func()->init();
269}
270
271
278
283{
284 Q_D(QSlider);
285 QStylePainter p(this);
286 QStyleOptionSlider opt;
288
290 if (d->tickPosition != NoTicks)
291 opt.subControls |= QStyle::SC_SliderTickmarks;
292
293 p.drawComplexControl(QStyle::CC_Slider, opt);
294}
295
301{
302 Q_D(QSlider);
303
304 switch(event->type()) {
308 if (const QHoverEvent *he = static_cast<const QHoverEvent *>(event))
309 d->updateHoverControl(he->position().toPoint());
310 break;
313 d->resetLayoutItemMargins();
314 break;
315 default:
316 break;
317 }
319}
320
325{
326 Q_D(QSlider);
327 if (d->maximum == d->minimum || (ev->buttons() ^ ev->button())) {
328 ev->ignore();
329 return;
330 }
331#ifdef QT_KEYPAD_NAVIGATION
332 if (QApplicationPrivate::keypadNavigationEnabled())
333 setEditFocus(true);
334#endif
335 ev->accept();
336 if ((ev->button() & style()->styleHint(QStyle::SH_Slider_AbsoluteSetButtons)) == ev->button()) {
337 QStyleOptionSlider opt;
340 const QPoint center = sliderRect.center() - sliderRect.topLeft();
341 // to take half of the slider off for the setSliderPosition call we use the center - topLeft
342
343 setSliderPosition(d->pixelPosToRangeValue(d->pick(ev->position().toPoint() - center)));
346 d->pressedControl = QStyle::SC_SliderHandle;
347 update();
348 } else if ((ev->button() & style()->styleHint(QStyle::SH_Slider_PageSetButtons)) == ev->button()) {
349 QStyleOptionSlider opt;
351 d->pressedControl = style()->hitTestComplexControl(QStyle::CC_Slider,
352 &opt, ev->position().toPoint(), this);
354 if (d->pressedControl == QStyle::SC_SliderGroove) {
356 int pressValue = d->pixelPosToRangeValue(d->pick(ev->position().toPoint() - sliderRect.center() + sliderRect.topLeft()));
357 d->pressValue = pressValue;
358 if (pressValue > d->value)
359 action = SliderPageStepAdd;
360 else if (pressValue < d->value)
361 action = SliderPageStepSub;
362 if (action) {
363 triggerAction(action);
364 setRepeatAction(action);
365 }
366 }
367 } else {
368 ev->ignore();
369 return;
370 }
371
372 if (d->pressedControl == QStyle::SC_SliderHandle) {
373 QStyleOptionSlider opt;
377 d->clickOffset = d->pick(ev->position().toPoint() - sr.topLeft());
378 update(sr);
379 setSliderDown(true);
380 }
381}
382
387{
388 Q_D(QSlider);
389 if (d->pressedControl != QStyle::SC_SliderHandle) {
390 ev->ignore();
391 return;
392 }
393 ev->accept();
394 int newPosition = d->pixelPosToRangeValue(d->pick(ev->position().toPoint()) - d->clickOffset);
395 setSliderPosition(newPosition);
396}
397
398
403{
404 Q_D(QSlider);
405 if (d->pressedControl == QStyle::SC_None || ev->buttons()) {
406 ev->ignore();
407 return;
408 }
409 ev->accept();
410 QStyle::SubControl oldPressed = QStyle::SubControl(d->pressedControl);
411 d->pressedControl = QStyle::SC_None;
413 if (oldPressed == QStyle::SC_SliderHandle)
414 setSliderDown(false);
415 QStyleOptionSlider opt;
417 opt.subControls = oldPressed;
418 update(style()->subControlRect(QStyle::CC_Slider, &opt, oldPressed, this));
419}
420
425{
426 Q_D(const QSlider);
428 const int SliderLength = 84, TickSpace = 5;
429 QStyleOptionSlider opt;
431 int thick = style()->pixelMetric(QStyle::PM_SliderThickness, &opt, this);
432 if (d->tickPosition & TicksAbove)
433 thick += TickSpace;
434 if (d->tickPosition & TicksBelow)
435 thick += TickSpace;
436 int w = thick, h = SliderLength;
437 if (d->orientation == Qt::Horizontal) {
438 w = SliderLength;
439 h = thick;
440 }
441 return style()->sizeFromContents(QStyle::CT_Slider, &opt, QSize(w, h), this);
442}
443
448{
449 Q_D(const QSlider);
450 QSize s = sizeHint();
451 QStyleOptionSlider opt;
454 if (d->orientation == Qt::Horizontal)
455 s.setWidth(length);
456 else
457 s.setHeight(length);
458 return s;
459}
460
473{
474 Q_D(QSlider);
475 d->tickPosition = position;
476 d->resetLayoutItemMargins();
477 update();
479}
480
482{
483 return d_func()->tickPosition;
484}
485
499{
500 d_func()->tickInterval = qMax(0, ts);
501 update();
502}
503
505{
506 return d_func()->tickInterval;
507}
508
509Q_WIDGETS_EXPORT QStyleOptionSlider qt_qsliderStyleOption(QSlider *slider)
510{
511 QStyleOptionSlider sliderOption;
512 slider->initStyleOption(&sliderOption);
513 return sliderOption;
514}
515
517
518#include "moc_qslider.cpp"
Qt::Orientation orientation
The QAbstractSlider class provides an integer value within a range.
void setRepeatAction(SliderAction action, int thresholdTime=500, int repeatTime=50)
Sets action action to be triggered repetitively in intervals of repeatTime, after an initial delay of...
SliderAction
\value SliderNoAction \value SliderSingleStepAdd \value SliderSingleStepSub \value SliderPageStepAdd ...
Qt::Orientation orientation
the orientation of the slider
bool event(QEvent *e) override
\reimp
void setSliderPosition(int)
void triggerAction(SliderAction action)
Triggers a slider action.
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleChange
Definition qcoreevent.h:136
@ HoverLeave
Definition qcoreevent.h:176
@ HoverEnter
Definition qcoreevent.h:175
@ HoverMove
Definition qcoreevent.h:177
@ MacSizeChange
Definition qcoreevent.h:217
\inmodule QtGui
Definition qevent.h:246
\inmodule QtGui
Definition qevent.h:196
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
\inmodule QtCore
Definition qsize.h:25
QStyle::SubControl hoverControl
Definition qslider.cpp:35
QSlider::TickPosition tickPosition
Definition qslider.cpp:26
int pixelPosToRangeValue(int pos) const
Definition qslider.cpp:65
QStyle::SubControl newHoverControl(const QPoint &pos)
Definition qslider.cpp:146
int pick(const QPoint &pt) const
Definition qslider.cpp:87
void resetLayoutItemMargins()
Definition qslider.cpp:55
bool updateHoverControl(const QPoint &pos)
Definition qslider.cpp:132
QRect hoverRect
Definition qslider.cpp:36
QStyle::SubControl pressedControl
Definition qslider.cpp:24
The QSlider widget provides a vertical or horizontal slider.
Definition qslider.h:18
TickPosition
This enum specifies where the tick marks are to be drawn relative to the slider's groove and the hand...
Definition qslider.h:25
@ TicksAbove
Definition qslider.h:27
@ TicksBelow
Definition qslider.h:29
@ NoTicks
Definition qslider.h:26
void setTickInterval(int ti)
Definition qslider.cpp:498
~QSlider()
Destroys this slider.
Definition qslider.cpp:275
void mouseReleaseEvent(QMouseEvent *ev) override
\reimp
Definition qslider.cpp:402
void mousePressEvent(QMouseEvent *ev) override
\reimp
Definition qslider.cpp:324
bool event(QEvent *event) override
\reimp
Definition qslider.cpp:300
QSize minimumSizeHint() const override
\reimp
Definition qslider.cpp:447
void setTickPosition(TickPosition position)
Definition qslider.cpp:472
virtual void initStyleOption(QStyleOptionSlider *option) const
Initialize option with the values from this QSlider.
Definition qslider.cpp:99
void paintEvent(QPaintEvent *ev) override
\reimp
Definition qslider.cpp:282
QSlider(QWidget *parent=nullptr)
Constructs a vertical slider with the given parent.
Definition qslider.cpp:253
TickPosition tickPosition
the tickmark position for this slider
Definition qslider.h:21
void mouseMoveEvent(QMouseEvent *ev) override
\reimp
Definition qslider.cpp:386
QSize sizeHint() const override
\reimp
Definition qslider.cpp:424
int tickInterval
the interval between tickmarks
Definition qslider.h:22
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
@ State_Sunken
Definition qstyle.h:69
@ State_Horizontal
Definition qstyle.h:74
@ CT_Slider
Definition qstyle.h:559
virtual QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget=nullptr) const =0
Returns the rectangle containing the specified subControl of the given complex control (with the styl...
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=nullptr) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
@ SH_Slider_PageSetButtons
Definition qstyle.h:650
@ SH_Slider_AbsoluteSetButtons
Definition qstyle.h:649
@ SH_Button_FocusPolicy
Definition qstyle.h:634
virtual SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt, const QWidget *widget=nullptr) const =0
Returns the sub control at the given position in the given complex control (with the style options sp...
@ PM_SliderThickness
Definition qstyle.h:429
@ PM_SliderLength
Definition qstyle.h:431
static int sliderValueFromPosition(int min, int max, int pos, int space, bool upsideDown=false)
Converts the given pixel position to a logical value.
Definition qstyle.cpp:2267
@ CC_Slider
Definition qstyle.h:335
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
Returns the value of the given pixel metric.
@ SE_SliderLayoutItem
Definition qstyle.h:296
SubControl
This enum describes the available sub controls.
Definition qstyle.h:347
@ SC_SliderGroove
Definition qstyle.h:369
@ SC_All
Definition qstyle.h:400
@ SC_None
Definition qstyle.h:348
@ SC_SliderTickmarks
Definition qstyle.h:371
@ SC_SliderHandle
Definition qstyle.h:370
void setLayoutItemMargins(int left, int top, int right, int bottom)
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void updateGeometry()
Notifies the layout system that this widget has changed and may need to change geometry.
void ensurePolished() const
Ensures that the widget and its children have been polished by QStyle (i.e., have a proper font and p...
void update()
Updates the widget unless updates are disabled or the widget is hidden.
QStyle * style() const
Definition qwidget.cpp:2600
QStyleOptionButton opt
Combined button and popup list for selecting options.
Definition qcompare.h:63
@ WA_Hover
Definition qnamespace.h:340
@ WA_WState_OwnSizePolicy
Definition qnamespace.h:334
@ LeftToRight
@ RightToLeft
FocusPolicy
Definition qnamespace.h:106
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLenum GLsizei length
GLfloat GLfloat GLfloat GLfloat h
struct _cl_event * event
GLdouble s
[6]
Definition qopenglext.h:235
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
GLuint GLenum option
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
Q_WIDGETS_EXPORT QStyleOptionSlider qt_qsliderStyleOption(QSlider *slider)
Definition qslider.cpp:509
#define sp