QT6 源(80):阅读与注释进度条 QProgressBar 的源码,其是界面输出类,是 QWidget 的子类。以及属性验证:忙碌状态,格式化输出 %p、%v、%m,reset()重置进度条

(1)

在这里插入图片描述

(2) 格式化输出 %p、%v、%m

在这里插入图片描述

(3) reset()重置进度条

在这里插入图片描述

(4) 本源代码来自于头文件 qprogressbar . h

#ifndef QPROGRESSBAR_H
#define QPROGRESSBAR_H

#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qframe.h>

QT_REQUIRE_CONFIG(progressbar);

QT_BEGIN_NAMESPACE

class QProgressBarPrivate;
class QStyleOptionProgressBar;

/*
The QProgressBar widget provides a horizontal or vertical progress bar.

进度条用于向用户指示操作的进度,并确保他们知道应用程序仍在运行。

The progress bar uses the concept of steps. You set it up by specifying the
minimum and maximum possible step values, and it will display the percentage of
steps that have been completed when you later give it the current step value.
The percentage is calculated by dividing the progress (value() - minimum()) divided by
maximum() - minimum().

您可以使用 setMinimum()和 setMaximum()、指定最小和最大步数。使用setValue()、设置当前步数。
使用 reset() 将进度条回滚到开始。
如果最小值和最大值都设置为0,则条形图将显示繁忙指示器而不是步骤百分比。
例如,当使用QNetworkAccessManager下载项目并且无法确定正在下载的项目的大小时,这非常有用。

*/

class Q_WIDGETS_EXPORT QProgressBar : public QWidget //进度条,这个翻译更准确
{
    Q_OBJECT

    //This property holds the progress bar's minimum value。
    //When setting this property, the maximum is adjusted if necessary to
    //ensure that the range remains valid. If the current value falls outside the
    //new range, the progress bar is reset with reset().
    Q_PROPERTY(int minimum READ minimum WRITE setMinimum)

    Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
    //This property holds the progress bar's maximum value

    Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
    //This property holds the progress bar's current value。
    //Attempting to change the current value to one outside the
    //minimum-maximum range has no effect on the current value.

    //此属性包含与进度条一起显示的说明性文本。
    //返回的文本与进度条中心(或在某些样式中,左侧)显示的文本相同。
    //文本中显示的进度可能小于最小值,这表明进度条在设置任何进度之前处于“重置”状态。
    //在默认实现中,文本要么包含表示迄今为止的进度的百分比值,要么为空,因为进度条处于重置状态。
    Q_PROPERTY(QString   text          READ text)

    Q_PROPERTY(bool      textVisible   READ isTextVisible WRITE setTextVisible)
    //This property holds whether the current completed percentage should be displayed。
    //This property may be ignored by the style (e.g., QMacStyle never draws the text).

    Q_PROPERTY(QString   format        READ format WRITE setFormat RESET resetFormat)
    //This property holds the string used to generate the current text。
    //%p - is replaced by the percentage completed.  即 (val - min)/(max -min)
    //%v - is replaced by the current value.         即  val
    //%m - is replaced by the total number of steps. 即 (max - min)
    //The default value is "%p%".

    Q_PROPERTY(Direction textDirection READ textDirection WRITE setTextDirection)
    //This property holds the reading direction of the text for vertical progress bars.
    //This property has no impact on horizontal progress bars. //本属性只对垂直进度条有用
    //By default, the reading direction is QProgressBar::TopToBottom. //默认从上到下

    // /This property holds the alignment of the progress bar
    Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
/*  //             Alignment = QFlags<AlignmentFlag> 这是枚举类
    enum AlignmentFlag  //对齐策略,删减版
    {
        AlignLeft     = 0x0001,
        AlignRight    = 0x0002,
        AlignHCenter  = 0x0004,     //水平中心对齐
        AlignTop      = 0x0020,
        AlignBottom   = 0x0040,
        AlignVCenter  = 0x0080,
        AlignCenter = AlignVCenter | AlignHCenter //水平与垂直居中
    };
*/

    Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
    //This property holds the orientation of the progress bar。 //水平或垂直进度条
    //The orientation must be Qt::Horizontal (the default) or Qt::Vertical.

    Q_PROPERTY(bool  invertedAppearance
                READ invertedAppearance WRITE setInvertedAppearance)
    //This property holds whether or not a progress bar shows its progress inverted。
    //If this property is true, the progress bar grows in the other direction
    //(e.g. from right to left). By default, the progress bar is not inverted.

private:
    Q_DECLARE_PRIVATE(QProgressBar)
    Q_DISABLE_COPY(QProgressBar)

public:
    enum Direction { TopToBottom, BottomToTop }; //本枚举类用于属性的赋值
    Q_ENUM(Direction) //接入 QT的元对象系统

    //Constructs a progress bar with the given parent.
    //By default, the minimum step value is set to 0, and the maximum to 100.
    explicit QProgressBar(QWidget * parent = nullptr);

    ~QProgressBar();

//Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
        int      minimum() const;
public Q_SLOTS:
        void  setMinimum(int minimum);

public :
//Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
        int      maximum() const;
public Q_SLOTS:
        void  setMaximum(int maximum);

public :
//Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
        int      value() const;
public Q_SLOTS:
        void  setValue(int value);
Q_SIGNALS:
        void     valueChanged(int value);

public :
//Q_PROPERTY(QString   text          READ text)
    virtual  QString   text() const;

//Q_PROPERTY(bool      textVisible   READ isTextVisible WRITE setTextVisible)
            bool     isTextVisible() const;
            void    setTextVisible(bool visible);

//Q_PROPERTY(QString   format        READ format WRITE setFormat RESET resetFormat)
        QString        format() const;
        void        setFormat(const QString & format);
public Q_SLOTS:
        void      resetFormat(); //The default value is "%p%".

public :
//Q_PROPERTY(Direction       textDirection
//                READ       textDirection WRITE setTextDirection)
    QProgressBar::Direction  textDirection() const;
                    void  setTextDirection(QProgressBar::Direction textDirection);

//Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
        Qt::Alignment      alignment() const;  //文本 text的复杂的对齐方式
        void            setAlignment(Qt::Alignment alignment);

//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 invert);


        QSize        sizeHint() const override;
        QSize minimumSizeHint() const override;

public Q_SLOTS:
    void      setRange(int minimum, int maximum);

    //Reset the progress bar. The progress bar "rewinds" and shows no progress.
    void    reset(); //重置进度条。进度条“倒带”并显示没有进度。

    //void    setMinimum(int minimum);
    //void    setMaximum(int maximum);
    //void    setValue  (int value);
    //void    setOrientation(Qt::Orientation); //进度条的水平或垂直

Q_SIGNALS:
    //void valueChanged(int value);

protected:
    virtual void initStyleOption(QStyleOptionProgressBar *option) const;

    bool      event(QEvent      * e) override;
    void paintEvent(QPaintEvent *  ) override;

}; //完结 class QProgressBar : public QWidget //进度条,这个翻译更准确

QT_END_NAMESPACE

#endif // QPROGRESSBAR_H

(5)

谢谢

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangzhangkeji

谢谢支持

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

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

打赏作者

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

抵扣说明:

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

余额充值