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_NAMESPACE

class 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 QAbstractSlider

QT_END_NAMESPACE

#endif // QSLIDER_H

(2)

在这里插入图片描述

(3)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值