QT6 源(78):阅读与注释滑动条 QSlider 的源码,其是基类QAbstractSlider 的子类,及其刻度线的属性举例
(1)本源码在头文件 qslider . h :
#ifndef QSLIDER_H
#define QSLIDER_H#include <QtWidgets/qtwidgetsglobal.h>#include <QtWidgets/qabstractslider.h>QT_REQUIRE_CONFIG(slider);QT_BEGIN_NAMESPACEclass QSliderPrivate;
class QStyleOptionSlider;/*
The QSlider widget provides a vertical or horizontal slider.滑块是控制有界值的经典小部件。
它允许用户将滑块手柄沿水平或垂直槽移动,并将手柄的位置转换为合法范围内的整数值。QSlider自身的功能很少,大部分功能都在QAbstractslider中。
最有用的函数是setValue(),用于将滑块直接设置为某个值;
triggerAction()用于模拟单击的效果(对快捷键很有用);
setSingleStep()、setPageStep()用于设置步骤 steps ;
以及setMinimum()和setMaximum()用于定义滚动条的范围。QSlider提供了控制刻度线的方法。您可以使用setTickPosition()来指示您希望刻度线的位置,
setTicklnterval() 来指示您想要的刻度线数量。
the currently set tick position and interval can be queried using the
tickPosition() and tickInterval() functions, respectively。QSlider only provides integer ranges.
Note that although QSlider handles very large numbers,
it becomes difficult for users to use a slider accurately for very large ranges.
A slider accepts focus on Tab and provides both a mouse wheel and a
keyboard interface. The keyboard interface is the following:Left/Right move a horizontal slider by one single step.Up/Down move a vertical slider by one single step.PageUp moves up one page.PageDown moves down one page.Home moves to the start (minimum).End moves to the end (maximum).*/class Q_WIDGETS_EXPORT QSlider : public QAbstractSlider
{Q_OBJECT//该枚举类的定义就在下方。//This property holds the tickmark position for this slider。//The valid values are described by the QSlider::TickPosition enum.// The default value is QSlider::NoTicks. //默认不显示滑动条的刻度线Q_PROPERTY(TickPosition tickPosition READ tickPosition WRITE setTickPosition)Q_PROPERTY(int tickInterval READ tickInterval WRITE setTickInterval)//This property holds the interval between tickmarks。//This is a value interval, not a pixel interval.//If it is 0, the slider will choose between singleStep and pageStep.// The default value is 0.private:friend Q_WIDGETS_EXPORT //友元函数QStyleOptionSlider qt_qsliderStyleOption(QSlider *slider);Q_DISABLE_COPY(QSlider)Q_DECLARE_PRIVATE(QSlider)public://Constructs a vertical slider with the given parent. 看来默认创建垂直滑动条explicit QSlider(QWidget * parent = nullptr);explicit QSlider(Qt::Orientation orientation, QWidget * parent = nullptr);//Constructs a slider with the given parent.//The orientation parameter determines whether the slider is horizontal or//vertical; the valid values are Qt::Vertical and Qt::Horizontal.~QSlider();enum TickPosition {NoTicks = 0,TicksAbove = 1,TicksLeft = TicksAbove,TicksBelow = 2,TicksRight = TicksBelow,TicksBothSides = 3};Q_ENUM(TickPosition) //把本枚举类接入元对象系统//Q_PROPERTY(TickPosition tickPosition READ tickPosition WRITE setTickPosition)TickPosition tickPosition() const;void setTickPosition(TickPosition position);//Q_PROPERTY(int tickInterval READ tickInterval WRITE setTickInterval)int tickInterval() const;void setTickInterval(int ti);QSize sizeHint() const override;QSize minimumSizeHint() const override;bool event(QEvent * event) override;
protected:void paintEvent(QPaintEvent * ev ) override;void mousePressEvent(QMouseEvent * ev ) override;void mouseReleaseEvent(QMouseEvent * ev ) override;void mouseMoveEvent(QMouseEvent * ev ) override;virtual void initStyleOption(QStyleOptionSlider * option) const;}; //完结 class QSlider : public QAbstractSliderQT_END_NAMESPACE#endif // QSLIDER_H
(2)
(3)
谢谢