Qt編寫自定義控件14-環形進度條

前言

環形進度條,用來展現當前進度,爲了知足大屏UI的須要特地定製,之前有個叫圓環進度條,不能知足項目須要,只能從新定作,之前的進度間距不能自適應分辨率,並且當前進度對應的反的進度不能單獨設置顏色,即當前進度90%,剩餘的10%也須要設置成不一樣的顏色,還有一個重要的功能是,可以指定多個警惕值,一旦超過或者小於該值,則當前進度自動切換到預先設定的警惕值顏色,而不須要用戶本身去判斷警惕值去設置警惕顏色,用戶只須要傳入當前值便可,這個功能很是實用,還能夠設置警惕判斷的標準是超過值仍是小於值報警。我的感受這個環形進度條功能完爆市面上全部的圓環進度條。只要稍做參數設置能夠變成各類想要的效果,什麼起始角度+動畫效果+順時針逆時針轉等。c++

實現的功能

  • 1:可設置範圍值,支持負數值
  • 2:可設置精確度,最大支持小數點後3位
  • 3:可設置起始角度
  • 4:可設置三種值+三種顏色,啓用自動檢測值後繪製不一樣的顏色
  • 5:可設置是否啓用動畫效果以及動畫效果每次移動的步長
  • 6:可設置背景顏色/文字顏色/進度顏色/中間圓顏色
  • 7:可設置值警惕報警比較模式 0-不比較 1-最大值報警 2-最小值報警
  • 8:可設置顯示的值是百分比
  • 9:可設置圓環與背景之間的距離即間距
  • 10:可設置圓環的寬度
  • 11:可設置圓環背景顏色,造成兩種顏色差
  • 12:可設置順時針逆時針轉
  • 13:自適應窗體拉伸,刻度尺和文字自動縮放

效果圖

頭文件代碼

#ifndef PROGRESSRING_H
#define PROGRESSRING_H

/**
 * 環形進度條控件 做者:feiyangqingyun(QQ:517216493) 2019-5-1
 * 1:可設置範圍值,支持負數值
 * 2:可設置精確度,最大支持小數點後3位
 * 3:可設置起始角度
 * 4:可設置三種值+三種顏色,啓用自動檢測值後繪製不一樣的顏色
 * 5:可設置是否啓用動畫效果以及動畫效果每次移動的步長
 * 6:可設置背景顏色/文字顏色/進度顏色/中間圓顏色
 * 7:可設置值警惕報警比較模式 0-不比較 1-最大值報警 2-最小值報警
 * 8:可設置顯示的值是百分比
 * 9:可設置圓環與背景之間的距離即間距
 * 10:可設置圓環的寬度
 * 11:可設置圓環背景顏色,造成兩種顏色差
 * 12:可設置順時針逆時針轉
 * 13:自適應窗體拉伸,刻度尺和文字自動縮放
 */

#include <QWidget>

#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif

class QDESIGNER_WIDGET_EXPORT ProgressRing : public QWidget
#else
class ProgressRing : public QWidget
#endif

{
    Q_OBJECT
    Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
    Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
    Q_PROPERTY(double value READ getValue WRITE setValue)
    Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)

    Q_PROPERTY(bool showPercent READ getShowPercent WRITE setShowPercent)
    Q_PROPERTY(int alarmMode READ getAlarmMode WRITE setAlarmMode)
    Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle)
    Q_PROPERTY(int ringPadding READ getRingPadding WRITE setRingPadding)
    Q_PROPERTY(int ringWidth READ getRingWidth WRITE setRingWidth)

    Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
    Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)

    Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor)
    Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
    Q_PROPERTY(QColor ringColor READ getRingColor WRITE setRingColor)
    Q_PROPERTY(QColor ringBgColor READ getRingBgColor WRITE setRingBgColor)
    Q_PROPERTY(QColor circleColor READ getCircleColor WRITE setCircleColor)

    Q_PROPERTY(int ringValue1 READ getRingValue1 WRITE setRingValue1)
    Q_PROPERTY(int ringValue2 READ getRingValue2 WRITE setRingValue2)
    Q_PROPERTY(int ringValue3 READ getRingValue3 WRITE setRingValue3)

    Q_PROPERTY(QColor ringColor1 READ getRingColor1 WRITE setRingColor1)
    Q_PROPERTY(QColor ringColor2 READ getRingColor2 WRITE setRingColor2)
    Q_PROPERTY(QColor ringColor3 READ getRingColor3 WRITE setRingColor3)

public:
    explicit ProgressRing(QWidget *parent = 0);
    ~ProgressRing();

protected:
    void paintEvent(QPaintEvent *);
    void drawBg(QPainter *painter);
    void drawRing(QPainter *painter);
    void drawPadding(QPainter *painter);
    void drawCircle(QPainter *painter);
    void drawValue(QPainter *painter);

private slots:
    void updateValue();

private:
    double minValue;                //最小值
    double maxValue;                //最大值
    double value;                   //目標值
    int precision;                  //精確度,小數點後幾位

    bool clockWise;                 //順時針逆時針
    bool showPercent;               //顯示百分比
    int alarmMode;                  //警惕報警模式,進度爲不一樣的顏色
    int startAngle;                 //起始角度
    int ringPadding;                //圓環間距
    int ringWidth;                  //圓環寬度

    bool animation;                 //是否啓用動畫顯示
    double animationStep;           //動畫顯示時步長

    QColor bgColor;                 //背景顏色
    QColor textColor;               //文字顏色
    QColor ringColor;               //圓環顏色
    QColor ringBgColor;             //圓環進度背景
    QColor circleColor;             //中心圓顏色

    int ringValue1;                 //環形值1
    int ringValue2;                 //環形值2
    int ringValue3;                 //環形值3

    QColor ringColor1;              //環形顏色1
    QColor ringColor2;              //環形顏色2
    QColor ringColor3;              //環形顏色3

    bool reverse;                   //是否往回走
    double currentValue;            //當前值
    QTimer *timer;                  //定時器繪製動畫

public:
    double getMinValue()            const;
    double getMaxValue()            const;
    double getValue()               const;
    int getPrecision()              const;

    bool getClockWise()             const;
    bool getShowPercent()           const;
    int getAlarmMode()              const;
    int getStartAngle()             const;
    int getRingPadding()            const;
    int getRingWidth()              const;

    bool getAnimation()             const;
    double getAnimationStep()       const;

    QColor getBgColor()             const;
    QColor getTextColor()           const;
    QColor getRingColor()           const;
    QColor getRingBgColor()         const;
    QColor getCircleColor()         const;

    int getRingValue1()             const;
    int getRingValue2()             const;
    int getRingValue3()             const;

    QColor getRingColor1()          const;
    QColor getRingColor2()          const;
    QColor getRingColor3()          const;

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

public Q_SLOTS:
    //設置範圍值
    void setRange(double minValue, double maxValue);
    void setRange(int minValue, int maxValue);

    //設置最大最小值
    void setMinValue(double minValue);
    void setMaxValue(double maxValue);

    //設置目標值
    void setValue(double value);
    void setValue(int value);

    //設置精確度
    void setPrecision(int precision);

    //設置順時針逆時針轉
    void setClockWise(bool clockWise);
    //設置顯示百分比
    void setShowPercent(bool showPercent);
    //設置啓動自動檢驗
    void setAlarmMode(int alarmMode);
    //設置起始角度
    void setStartAngle(int startAngle);
    //設置圓環間距
    void setRingPadding(int ringPadding);
    //設置圓環寬度
    void setRingWidth(int ringWidth);

    //設置是否啓用動畫顯示
    void setAnimation(bool animation);
    //設置動畫顯示的步長
    void setAnimationStep(double animationStep);

    //設置背景顏色
    void setBgColor(const QColor &bgColor);
    //設置文本顏色
    void setTextColor(const QColor &textColor);
    //設置圓環進度顏色
    void setRingColor(const QColor &ringColor);
    //設置圓環背景顏色
    void setRingBgColor(const QColor &ringBgColor);
    //設置中心圓顏色
    void setCircleColor(const QColor &circleColor);

    //設置三種值
    void setRingValue1(int ringValue1);
    void setRingValue2(int ringValue2);
    void setRingValue3(int ringValue3);

    //設置三種顏色
    void setRingColor1(const QColor &ringColor1);
    void setRingColor2(const QColor &ringColor2);
    void setRingColor3(const QColor &ringColor3);

Q_SIGNALS:
    void valueChanged(int value);
};

#endif // PROGRESSRING_H

核心代碼

void ProgressRing::paintEvent(QPaintEvent *)
{
    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    //繪製準備工做,啓用反鋸齒,平移座標軸中心,等比例縮放
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width / 2, height / 2);
    painter.scale(side / 200.0, side / 200.0);

    //繪製背景
    drawBg(&painter);
    //繪製進度
    drawRing(&painter);

    //繪製間隔,從新繪製一個圓遮住,產生間距效果
    if (ringPadding > 0) {
        drawPadding(&painter);
    }

    //繪製中間圓
    drawCircle(&painter);
    //繪製當前值
    drawValue(&painter);
}

void ProgressRing::drawBg(QPainter *painter)
{
    int radius = 99;
    painter->save();
    painter->setPen(Qt::NoPen);
    //這裏有個技巧,若是沒有間距則設置成圓環的背景色
    painter->setBrush(ringPadding == 0 ? ringBgColor : bgColor);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void ProgressRing::drawRing(QPainter *painter)
{
    int radius = 99 - ringPadding;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(ringColor);

    QRectF rect(-radius, -radius, radius * 2, radius * 2);

    //計算總範圍角度,當前值範圍角度,剩餘值範圍角度
    double angleAll = 360.0;
    double angleCurrent = angleAll * ((currentValue - minValue) / (maxValue - minValue));
    double angleOther = angleAll - angleCurrent;

    //若是逆時針
    if (!clockWise) {
        angleCurrent = -angleCurrent;
        angleOther = -angleOther;
    }

    //動態設置當前進度顏色
    QColor color = ringColor;
    if (alarmMode == 1) {
        if (currentValue >= ringValue3) {
            color = ringColor3;
        } else if (currentValue >= ringValue2) {
            color = ringColor2;
        } else {
            color = ringColor1;
        }
    } else if (alarmMode == 2) {
        if (currentValue <= ringValue1) {
            color = ringColor1;
        } else if (currentValue <= ringValue2) {
            color = ringColor2;
        } else {
            color = ringColor3;
        }
    }

    //繪製當前值餅圓
    painter->setBrush(color);
    painter->drawPie(rect, (startAngle - angleCurrent) * 16, angleCurrent * 16);

    //繪製剩餘值餅圓
    painter->setBrush(ringBgColor);
    painter->drawPie(rect, (startAngle - angleCurrent - angleOther) * 16, angleOther * 16);

    painter->restore();
}

void ProgressRing::drawPadding(QPainter *painter)
{
    int radius = 99 - ringWidth - ringPadding;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(bgColor);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void ProgressRing::drawCircle(QPainter *painter)
{
    //文字的區域要減去進度的寬度及間距
    int radius = 99 - ringWidth - (ringPadding * 2);
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(circleColor);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void ProgressRing::drawValue(QPainter *painter)
{
    //文字的區域要減去進度的寬度及間距
    int radius = 99 - ringWidth - (ringPadding * 2);
    painter->save();
    painter->setPen(textColor);

    QFont font;
    int fontSize = radius - (showPercent ? 20 : 6);
    font.setPixelSize(fontSize);
    painter->setFont(font);

    QRectF textRect(-radius, -radius, radius * 2, radius * 2);
    QString strValue;
    if (showPercent) {
        double percent = (currentValue * 100) / (maxValue - minValue);
        strValue = QString("%1%").arg(percent, 0, 'f', precision);
    } else {
        strValue = QString("%1").arg(currentValue, 0, 'f', precision);
    }

    painter->drawText(textRect, Qt::AlignCenter, strValue);

    painter->restore();
}

控件介紹

  1. 超過145個精美控件,涵蓋了各類儀表盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,flatui、高亮按鈕、滑動選擇器、農曆等。遠超qwt集成的控件數量。
  2. 每一個類均可以獨立成一個單獨的控件,零耦合,每一個控件一個頭文件和一個實現文件,不依賴其餘文件,方便單個控件以源碼形式集成到項目中,較少代碼量。qwt的控件類環環相扣,高度耦合,想要使用其中一個控件,必須包含全部的代碼。
  3. 所有純Qt編寫,QWidget+QPainter繪製,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等編譯器,不亂碼,可直接集成到Qt Creator中,和自帶的控件同樣使用,大部分效果只要設置幾個屬性便可,極爲方便。
  4. 每一個控件都有一個對應的單獨的包含該控件源碼的DEMO,方便參考使用。同時還提供一個全部控件使用的集成的DEMO。
  5. 每一個控件的源代碼都有詳細中文註釋,都按照統一設計規範編寫,方便學習自定義控件的編寫。
  6. 每一個控件默認配色和demo對應的配色都很是精美。
  7. 超過130個可見控件,6個不可見控件。
  8. 部分控件提供多種樣式風格選擇,多種指示器樣式選擇。
  9. 全部控件自適應窗體拉伸變化。
  10. 集成自定義控件屬性設計器,支持拖曳設計,所見即所得,支持導入導出xml格式。
  11. 自帶activex控件demo,全部控件能夠直接運行在ie瀏覽器中。
  12. 集成fontawesome圖形字體+阿里巴巴iconfont收藏的幾百個圖形字體,享受圖形字體帶來的樂趣。
  13. 全部控件最後生成一個dll動態庫文件,能夠直接集成到qtcreator中拖曳設計使用。

SDK下載

  • SDK下載連接:https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q 提取碼:lyhk
  • 自定義控件+屬性設計器欣賞:https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ 提取碼:tmvl
  • 下載連接中包含了各個版本的動態庫文件,全部控件的頭文件,使用demo。
  • 自定義控件插件開放動態庫dll使用(永久免費),無任何後門和限制,請放心使用。
  • 目前已提供22個版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
  • 不按期增長控件和完善控件,不按期更新SDK,歡迎各位提出建議,謝謝!
  • widget版本(QQ:517216493)qml版本(QQ:373955953)三峯駝(QQ:278969898)。

相關文章
相關標籤/搜索