(1)垂直滚(滑)动条中滑块所在的起始位置 :
(2)属性值 value 会同步更新滑动条中滑块的位置 :
(3) 滑动条确实是个输入部件 :
(4)
(5)以下源码来自于头文件 qabstractslider . h :
#ifndef QABSTRACTSLIDER_H
#define QABSTRACTSLIDER_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qwidget.h>
QT_REQUIRE_CONFIG(abstractslider);
QT_BEGIN_NAMESPACE //说明本类定义在 QT 的全局命名空间里
/*
The QAbstractSlider class provides an integer value within a range。
这个类被设计为像 QScrollBar、Qslider 和 QDial这样的小部件的通用超类。这里是类的主要属性:
1.value: OAbstractSlider 保持的有限整数。
2.最小值:可能的最小值。
3.最大值:可能的最大值。
4.singleStep:The smaller of two natural steps that an abstract sliders provides
and typically corresponds to the user pressing an arrow key。
5.pageStep :The larger of two natural steps that an abstract slider provides
and typically corresponds to the user pressing PageUp or PageDown。
6.跟踪:是否启用了滑块跟踪。
7.sliderPosition:滑块的当前位置。如果启用跟踪(默认为启用),则这与value相同。
Unity (1) may be viewed as a third step size。
setValue()允许您将当前值设置为允许范围内的任何整数,而不仅仅是整数n的最小值+n*singleStep()。
一些小部件可能允许用户设置任何值;其他小部件可能只提供 singleStep()或 pageStep()的倍数。
QAbstractSlider发出一个全面的一组信号:
QAbstractSlider emits a comprehensive set of signals:
valueChanged () the value has changed.The tracking determines whether this
signal is emitted during user interaction.
sliderPressed () the user starts to drag the slider.
sliderMoved () the user drags the slider.
sliderReleased () the user releases the slider.
actionTriggered() a slider action was triggered.
rangeChanged () a the range has changed.
QAbstractSlider提供了一个虚拟的sliderChange()函数,非常适合更新滑块在屏幕上的表示。
通过调用triggerAction(),子类可以触发滑块操作。
两个辅助函数 QStyle::sliderPositionFromValue() 和QStyle::sliderValueFromPosition()
帮助子类和样式将屏幕坐标映射到逻辑范围值。
*/
class QAbstractSliderPrivate;
class Q_WIDGETS_EXPORT QAbstractSlider : public QWidget
{
Q_OBJECT //又插入了本宏
//此属性保存滑块的最小值. 设置此属性时,如有必要,最大值会进行调整,以确保范围仍然有效。
//同时,滑块当前值也会调整到新范围内。
Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
//This property holds the slider's maximum value。
//This property holds the slider's current value。
//The slider forces the value to be within the legal range:
// minimum <= value <= maximum.
//Changing the value also changes the sliderPosition.
//值会同步到滑块的新位置。值会更新滑块的位置。但若为开启跟踪,滑块的实时位置并不同步于值。
Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged USER true)
Q_PROPERTY(int sliderPosition //滑块的位置,其实就等于 value属性
READ sliderPosition WRITE setSliderPosition NOTIFY sliderMoved)
//This property holds the current slider position。
//If tracking is enabled (the default), this is identical to value.
//This property holds the single step.
//If the property is modified during an auto repeating key event,
// behavior is undefined.
Q_PROPERTY(int singleStep READ singleStep WRITE setSingleStep)
Q_PROPERTY(int pageStep READ pageStep WRITE setPageStep)
//This property holds the page step.
//This property holds the orientation of the slider。
//The orientation must be Qt::Vertical (the default) or Qt::Horizontal.
//enum Qt::Orientation { Horizontal = 0x1, Vertical = 0x2 };
Q_PROPERTY(Qt::Orientation orientation //滑动条外观的水平方向或垂直方向
READ orientation WRITE setOrientation)
//此属性表示滑块是否显示倒置的值。
//如果此属性为假(默认值),则最小值和最大值将显示在继承的widget的经典位置。
//如果值为真,则最小值和最大值将出现在它们相反的位置。
//注意:对于滑块和仪表来说,此属性才有意义。
//对于滚动条,滚动条子控件的视觉效果取决于样式是否理解倒置外观;大多数样式忽略滚动条的此属性。
Q_PROPERTY(bool invertedAppearance //滑动条内部的正反向显示
READ invertedAppearance WRITE setInvertedAppearance)
Q_PROPERTY(bool invertedControls //上下箭头、page_up_down与数值增减的对应关系
READ invertedControls WRITE setInvertedControls)
//此属性表示滑块是否反转其轮盘和键事件。
//如果此属性为假,则滚动鼠标滚轮“向上”并使用“向上”等键会增加滑块的值,使其接近最大值。
//否则,按下“向上”键会将值移动到滑块的最低值。
//This property holds whether the slider is pressed down.
//The property is set by subclasses in order to let the abstract
//slider know whether or not tracking has any effect.
//Changing the slider down property emits the sliderPressed() and
// sliderReleased() signals.
Q_PROPERTY(bool sliderDown //表示滑动条按钮是否被按下
READ isSliderDown WRITE setSliderDown DESIGNABLE false)
//This property holds whether slider tracking is enabled。//默认启用 value对滑块的跟踪
//If tracking is enabled (the default), the slider emits the
// valueChanged() signal while the slider is being dragged.
//If tracking is disabled, the slider emits the
// valueChanged() signal only when the user releases the slider.
Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking) //关于信号发射的时机
private:
Q_DISABLE_COPY(QAbstractSlider)
Q_DECLARE_PRIVATE(QAbstractSlider)
protected:
QAbstractSlider(QAbstractSliderPrivate & dd, QWidget * parent = nullptr);
public:
explicit QAbstractSlider(QWidget * parent = nullptr);
//Constructs an abstract slider.
//The parent argument is sent to the QWidget constructor.
//The minimum defaults to 0, the maximum to 99,
//with a singleStep size of 1 and a pageStep size of 10,
//and an initial value of 0.
~QAbstractSlider();
//Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
int minimum() const;
void setMinimum(int);
//Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
int maximum() const;
void setMaximum(int);
//Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged USER true)
int value() const;
public Q_SLOTS:
void setValue(int);
//Changing the value also changes the sliderPosition.
Q_SIGNALS:
void valueChanged(int value);
//This signal is emitted when the slider value has changed,
//with the new slider value as argument.
public :
//Q_PROPERTY(int sliderPosition //滑块的位置,其实就等于 value属性
// READ sliderPosition WRITE setSliderPosition NOTIFY sliderMoved)
int sliderPosition() const;
void setSliderPosition(int);
Q_SIGNALS:
void sliderMoved(int position);
//This signal is emitted when sliderDown is true and the slider moves.
//This usually happens when the user is dragging the slider.
//The value is the new slider position. //本函数总是会实时发送滑块的位置,即使没启用跟踪
//This signal is emitted even when tracking is turned off.
public :
//Q_PROPERTY(int singleStep READ singleStep WRITE setSingleStep)
int singleStep() const;
void setSingleStep(int);
//Q_PROPERTY(int pageStep READ pageStep WRITE setPageStep)
int pageStep() const;
void setPageStep(int);
//Q_PROPERTY(Qt::Orientation orientation //滑动条外观的水平方向或垂直方向
// READ orientation WRITE setOrientation)
Qt::Orientation orientation() const;
public Q_SLOTS:
void setOrientation(Qt::Orientation);
public:
// Q_PROPERTY(bool invertedAppearance //滑动条内部的正反向显示
// READ invertedAppearance WRITE setInvertedAppearance)
bool invertedAppearance() const;
void setInvertedAppearance(bool);
// Q_PROPERTY(bool invertedControls //上下箭头、page_up_down与数值增减的对应关系
// READ invertedControls WRITE setInvertedControls)
bool invertedControls() const;
void setInvertedControls(bool);
// Q_PROPERTY(bool sliderDown //表示滑动条按钮是否被按下
// READ isSliderDown WRITE setSliderDown DESIGNABLE false)
bool isSliderDown() const;
void setSliderDown(bool);
//Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking) //关于信号发射的时机
bool hasTracking() const;
void setTracking(bool enable);
enum SliderAction { //无注释
SliderNoAction ,
SliderSingleStepAdd,
SliderSingleStepSub,
SliderPageStepAdd ,
SliderPageStepSub ,
SliderToMinimum ,
SliderToMaximum ,
SliderMove
};
//Triggers a slider action. Possible actions are SliderSingleStepAdd,
//SliderSingleStepSub, SliderPageStepAdd, SliderPageStepSub,
//SliderToMinimum, SliderToMaximum, and SliderMove.
void triggerAction(SliderAction action);
Q_SIGNALS: //本类定义好的信号函数
void actionTriggered(int action);
//This signal is emitted when the slider action action is triggered.
//Actions are SliderSingleStepAdd, SliderSingleStepSub, SliderPageStepAdd,
//SliderPageStepSub, SliderToMinimum, SliderToMaximum, and SliderMove.
//当发出信号时,滑块位置已经根据动作进行了调整,
//但值尚未传播(意味着valueChanged()信号尚未发出),视觉显示尚未更新。
//在连接到此信号的中您可以根据动作和滑块值安全地调用setSliderPosition()来调整任何动作。
//void valueChanged(int value) ; 未启用跟踪的情况下,只有值 value变了,才发送此信号
//void sliderMoved (int position); 只要滑块动了,就会发射此信号函数
void sliderPressed();
void sliderReleased();
void rangeChanged(int min, int max);
//This signal is emitted when the slider range has changed,
//with min being the new minimum, and max being the new maximum.
public Q_SLOTS: //本类定义好的槽函数
void setRange (int min, int max);
//void setValue(int);
//void setOrientation(Qt::Orientation);
protected: //保护类型,由子类调用
SliderAction repeatAction() const; //Returns the current repeat action.
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 thresholdTime.
enum SliderChange {
SliderRangeChange ,
SliderOrientationChange,
SliderStepsChange ,
SliderValueChange
};
virtual void sliderChange(SliderChange change);
//Reimplement this virtual function to track slider changes
//such as SliderRangeChange, SliderOrientationChange,
//SliderStepsChange, or SliderValueChange.
//The default implementation only updates the display and ignores the
//change parameter.
bool event(QEvent * e ) override;
void timerEvent(QTimerEvent * ) override;
void keyPressEvent(QKeyEvent * ev) override;
#if QT_CONFIG(wheelevent)
void wheelEvent(QWheelEvent * e ) override;
#endif
void changeEvent(QEvent * e ) override;
}; //完结 class QAbstractSlider : public QWidget
QT_END_NAMESPACE
#endif // QABSTRACTSLIDER_H
(6)
谢谢