Qt編寫自定義控件44-天氣儀表盤


一、前言

天氣儀表盤控件是所有控件中唯一一個使用了svg矢量圖的控件,各種天氣圖標采用的矢量圖,顏色變換采用動態載入svg的內容更改生成的,其實也可以采用圖形字體來做,本次控件為了熟悉下svg在Qt中的使用,才采用的svg來繪制。
天氣一般要表示多個內容,溫度+濕度+天氣等,這就需要合理的布局多種元素的位置才能更加美觀一些,這里參照的是網上一些通用的做法,比如最外層圓環是溫度,中間圓環濕度,然后天氣圖標貼在中間圓環里邊的左上角,同時再繪制溫度濕度的值,動態變化的。

二、實現的功能

  • 1:可設置兩種值,比如溫度+濕度
  • 2:可設置兩種值的背景顏色+文字顏色
  • 3:可設置零度值左側右側兩種顏色
  • 4:可設置圓的起始角度和結束角度
  • 5:可設置10種天氣,晴天+雨天+陰天+大風等
  • 6:可設置各種其他顏色
  • 7:科設置是否啟用動畫顯示進度以及動畫步長

三、效果圖

四、頭文件代碼

#ifndef GAUGEWEATHER_H
#define GAUGEWEATHER_H

/**
 * 天氣儀表盤控件 作者:東門吹雪(QQ:709102202) 整理:飛揚青雲(QQ:517216493) 2019-4-23
 * 1:可設置兩種值,比如溫度+濕度
 * 2:可設置兩種值的背景顏色+文字顏色
 * 3:可設置零度值左側右側兩種顏色
 * 4:可設置圓的起始角度和結束角度
 * 5:可設置10種天氣,晴天+雨天+陰天+大風等
 * 6:可設置各種其他顏色
 * 7:科設置是否啟用動畫顯示進度以及動畫步長
 */

#include <QWidget>
#include <QDomElement>

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

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

{
    Q_OBJECT
    Q_ENUMS(WeatherType)

    Q_PROPERTY(double outerValue READ getOuterValue WRITE setOuterValue)
    Q_PROPERTY(double outerMinValue READ getOuterMinValue WRITE setOuterMinValue)
    Q_PROPERTY(double outerMaxValue READ getOuterMaxValue WRITE setOuterMaxValue)

    Q_PROPERTY(int outerStartAngle READ getOuterStartAngle WRITE setOuterStartAngle)
    Q_PROPERTY(int outerEndAngle READ getOuterEndAngle WRITE setOuterEndAngle)

    Q_PROPERTY(QColor outerRingBgColor READ getOuterRingBgColor WRITE setOuterRingBgColor)
    Q_PROPERTY(QColor outerRingColor READ getOuterRingColor WRITE setOuterRingColor)

    Q_PROPERTY(double innerValue READ getInnerValue WRITE setInnerValue)
    Q_PROPERTY(double innerMinValue READ getInnerMinValue WRITE setInnerMinValue)
    Q_PROPERTY(double innerMaxValue READ getInnerMaxValue WRITE setInnerMaxValue)

    Q_PROPERTY(int innerStartAngle READ getInnerStartAngle WRITE setInnerStartAngle)
    Q_PROPERTY(int innerEndAngle READ getInnerEndAngle WRITE setInnerEndAngle)

    Q_PROPERTY(QColor innerRingBgColor READ getInnerRingBgColor WRITE setInnerRingBgColor)
    Q_PROPERTY(QColor innerRingNegativeColor READ getInnerNegativeColor WRITE setInnerNegativeColor)
    Q_PROPERTY(QColor innerRingPositiveColor READ getInnerPositiveColor WRITE setInnerPositiveColor)

    Q_PROPERTY(int innerScaleMajor READ getInnerScaleMajor WRITE setInnerScaleMajor)
    Q_PROPERTY(int innerScaleMinor READ getInnerScaleMinor WRITE setInnerScaleMinor)
    Q_PROPERTY(QColor innerScaleColor READ getInnerScaleColor WRITE setInnerScaleColor)
    Q_PROPERTY(QColor innerScaleNumColor READ getInnerScaleNumColor WRITE setInnerScaleNumColor)

    Q_PROPERTY(QColor centerPixMapNegativeColor READ getCenterPixMapNegativeColor WRITE setCenterPixMapNegativeColor)
    Q_PROPERTY(QColor centerPixMapPositiveColor READ getCenterPixMapPositiveColor WRITE setCenterPixMapPositiveColor)

    Q_PROPERTY(QColor outerValueTextColor READ getOuterValueTextColor WRITE setOuterValueTextColor)
    Q_PROPERTY(QColor innerNegativeValueTextColor READ getInnerNegativeValueTextColor WRITE setInnerNegativeValueTextColor)
    Q_PROPERTY(QColor innerPositiveValueTextColor READ getInnerPositiveValueTextColor WRITE setInnerPositiveValueTextColor)

    Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
    Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep)
    Q_PROPERTY(WeatherType weatherType READ getWeatherType WRITE setWeatherType)

public:
    enum WeatherType {
        SUNNY = 0,          //晴天
        RAINY = 1,          //雨天
        SNOWY = 2,          //雪天
        CLOUDY = 3,         //多雲
        WINDY = 4,          //風
        SNOWY_RAINY = 5,    //雪雨
        HAIL = 6,           //冰雹
        LIGHTNING = 7,      //閃電
        FOG = 8,            //霧
        PARTLYCLOUDY = 9    //局部多雲
    };

    explicit GaugeWeather(QWidget *parent = 0);
    ~GaugeWeather();

protected:
    void paintEvent(QPaintEvent *);
    void drawOuterRingBg(QPainter *painter);
    void drawOuterRing(QPainter *painter);
    void drawInnerRingBg(QPainter *painter);
    void drawInnerRing(QPainter *painter);
    void drawInnerRingScale(QPainter *painter);
    void drawInnerRingScaleNum(QPainter *painter);
    void drawCenterPixMap(QPainter *painter);
    void drawValue(QPainter *painter);

private slots:
    void updateOuterValue();            //更新外圈數值
    void updateInnerValue();            //更新內圈數值

private:
    double outerMaxValue;               //外圈最大值
    double outerMinValue;               //外圈最小值
    double outerValue;                  //外圈值
    double outerCurrValue;              //外圈當前值

    int outerStartAngle;                //外環開始旋轉角度
    int outerEndAngle;                  //外環結束旋轉角度

    QColor outerRingBgColor;            //外圈背景色
    QColor outerRingColor;              //外圈當前色

    double innerMaxValue;               //內圈最大值
    double innerMinValue;               //內圈最小值
    double innerValue;                  //內圈值
    double innerCurrValue;              //內圈當前值

    int innerStartAngle;                //內環開始旋轉角度
    int innerEndAngle;                  //內環結束旋轉角度

    QColor innerRingBgColor;            //內圈背景色
    QColor innerRingNegativeColor;      //內圈負值當前色
    QColor innerRingPositiveColor;      //內圈正值當前色

    int innerScaleMajor;                //內環大刻度數量
    int innerScaleMinor;                //內環小刻度數量
    QColor innerScaleColor;             //內環刻度顏色
    QColor innerScaleNumColor;          //內環刻度值顏色
    int precision;                      //精確度,小數點后幾位

    QColor centerPixMapNegativeColor;   //中心圖片顏色
    QColor centerPixMapPositiveColor;   //中心圖片顏色
    QString centerSvgPath;              //當前svg圖片路徑
    WeatherType weatherType;            //天氣類型

    QColor outerValueTextColor;         //外環值文本顏色
    QColor innerNegativeValueTextColor; //內環正值文本顏色
    QColor innerPositiveValueTextColor; //內環負值文本顏色

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

    bool outerReverse;                  //是否往回走
    bool innerReverse;                  //是否往回走
    bool clockWise;                     //順時針

    QTimer *outerTimer;                 //外環定時器繪制動畫
    QTimer *innerTimer;                 //內環定時器繪制動畫

    //將svg文件中的xml數據顏色替換
    void setColor(QDomElement elem, QString strtagname, QString strattr, QString strattrval);
    QString rgb2HexStr(const QColor &color);

public:
    double getOuterMinValue()               const;
    double getOuterMaxValue()               const;
    double getOuterValue()                  const;
    int getOuterStartAngle()                const;
    int getOuterEndAngle()                  const;

    QColor getOuterRingBgColor()            const;
    QColor getOuterRingColor()              const;

    double getInnerMaxValue()               const;
    double getInnerMinValue()               const;
    double getInnerValue()                  const;
    int getInnerStartAngle()                const;
    int getInnerEndAngle()                  const;

    QColor getInnerRingBgColor()            const;
    QColor getInnerNegativeColor()          const;
    QColor getInnerPositiveColor()          const;

    int getInnerScaleMajor()                const;
    int getInnerScaleMinor()                const;
    QColor getInnerScaleColor()             const;
    QColor getInnerScaleNumColor()          const;

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

    WeatherType getWeatherType()            const;

    QColor getCenterPixMapNegativeColor()   const;
    QColor getCenterPixMapPositiveColor()   const;

    QColor getOuterValueTextColor()         const;
    QColor getInnerNegativeValueTextColor() const;
    QColor getInnerPositiveValueTextColor() const;

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

public Q_SLOTS:
    void setWeatherType(WeatherType &type);

    //設置范圍值
    void setOuterRange(double minValue, double maxValue);
    //設置外環最大最小值
    void setOuterMinValue(double value);
    void setOuterMaxValue(double value);

    //設置外環值
    void setOuterValue(double value);
    //設置外環開始旋轉角度
    void setOuterStartAngle(int startAngle);
    //設置外環結束旋轉角度
    void setOuterEndAngle(int endAngle);
    //設置外環背景色
    void setOuterRingBgColor(const QColor &color);
    //設置外環進度色
    void setOuterRingColor(const QColor &color);

    //設置范圍值
    void setInnerRange(double minValue, double maxValue);
    void setInnerMinValue(double value);
    void setInnerMaxValue(double value);
    void setInnerValue(double value);
    void setInnerStartAngle(int startAngle);
    void setInnerEndAngle(int endAngle);

    void setInnerRingBgColor(const QColor &color);
    void setInnerNegativeColor(const QColor &color);
    void setInnerPositiveColor(const QColor &color);

    void setInnerScaleMajor(int value);
    void setInnerScaleMinor(int value);
    void setInnerScaleColor(const QColor &color);
    void setInnerScaleNumColor(const QColor &color);

    //設置中心圖標顏色
    void setCenterPixMapNegativeColor(const QColor &color);
    void setCenterPixMapPositiveColor(const QColor &color);

    void setOuterValueTextColor(const QColor &color);
    void setInnerNegativeValueTextColor(const QColor &color);
    void setInnerPositiveValueTextColor(const QColor &color);

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

Q_SIGNALS:
    void valueChanged(double value);
};

#endif // GAUGEWEATHER_H


五、核心代碼

void GaugeWeather::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);

    //繪制外環背景
    drawOuterRingBg(&painter);
    //繪制外環進度
    drawOuterRing(&painter);
    //繪制內環背景
    drawInnerRingBg(&painter);
    //繪制內環進度
    drawInnerRing(&painter);
    //繪制內環刻度值
    drawInnerRingScaleNum(&painter);
    //繪制中心圖片
    drawCenterPixMap(&painter);
    //繪制數值
    drawValue(&painter);
}

void GaugeWeather::drawOuterRingBg(QPainter *painter)
{
    int radius = 99;
    painter->save();
    painter->setBrush(Qt::NoBrush);

    //繪制圓弧方法繪制圓環
    int penWidth = 13;
    QRectF rect(-radius + penWidth / 2, -radius + penWidth / 2, radius * 2 - penWidth, radius * 2 - penWidth);
    //可以自行修改畫筆的后三個參數,形成各種各樣的效果,例如Qt::FlatCap改為Qt::RoundCap可以產生圓角效果
    QPen pen(outerRingBgColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);

    //計算總范圍角度
    double angleAll = 360.0 - outerStartAngle - outerEndAngle;

    //繪制總范圍角度圓弧
    pen.setColor(outerRingBgColor);
    painter->setPen(pen);
    painter->drawArc(rect, (270 - outerStartAngle - angleAll) * 16, angleAll * 16);
    painter->restore();
}

void GaugeWeather::drawOuterRing(QPainter *painter)
{
    int radius = 99;
    painter->save();
    painter->setBrush(Qt::NoBrush);

    //繪制圓弧方法繪制圓環
    int penWidth = 13;
    QRectF rect(-radius + penWidth / 2, -radius + penWidth / 2, radius * 2 - penWidth, radius * 2 - penWidth);
    //可以自行修改畫筆的后三個參數,形成各種各樣的效果,例如Qt::FlatCap改為Qt::RoundCap可以產生圓角效果
    QPen pen(outerRingColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);

    //計算總范圍角度,當前值范圍角度,剩余值范圍角度
    double angleAll = 360.0 - outerStartAngle - outerEndAngle;
    double angleCurrent = angleAll * ((outerCurrValue - outerMinValue) / (outerMaxValue - outerMinValue));

    //繪制當前值餅圓
    painter->setPen(pen);
    painter->drawArc(rect, (270 - outerStartAngle - angleCurrent) * 16, angleCurrent * 16);
    painter->restore();
}

void GaugeWeather::drawInnerRingBg(QPainter *painter)
{
    int radius = 77;
    painter->save();
    painter->setBrush(Qt::NoBrush);

    double penWidth = 13;
    QPen pen(innerRingBgColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);
    painter->setPen(pen);

    double angleAll = 360.0 - innerStartAngle - innerEndAngle;
    QRectF rect = QRectF(-radius, -radius, radius * 2, radius * 2);

    //零值以上背景
    painter->drawArc(rect, (270 - innerStartAngle -  angleAll) * 16 , angleAll * 16);
    //零值以下背景
    //painter->drawArc(rect,(270 -innerStartAngle -  null2MinAngle) * 16 ,null2MinAngle * 16);

    painter->restore();
}

void GaugeWeather::drawInnerRing(QPainter *painter)
{
    int radius = 77;
    painter->save();
    painter->setBrush(Qt::NoBrush);

    int penWidth = 13.5;
    QPen pen(innerRingPositiveColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);

    double angleAll = 360.0 - innerStartAngle - innerEndAngle;
    double null2MinAngle = angleAll * (( 0 - innerMinValue) / (innerMaxValue - innerMinValue));  //零點所占的角度
    double nullUpAllAngle = angleAll - null2MinAngle;   //正基本角度
    double currAngle;
    if(innerCurrValue >= 0) {
        //正角度
        pen.setColor(innerRingNegativeColor);
        currAngle = nullUpAllAngle * (innerCurrValue / innerMaxValue)  * -1;
    } else {
        //負角度
        pen.setColor(innerRingPositiveColor);
        currAngle = null2MinAngle * (innerCurrValue / innerMinValue);
    }

    painter->setPen(pen);

    QRectF rect = QRectF(-radius, -radius, radius * 2, radius * 2);
    painter->drawArc(rect, (270 - innerStartAngle -  null2MinAngle) * 16 , currAngle * 16);

    painter->restore();

}

void GaugeWeather::drawInnerRingScale(QPainter *painter)
{
    int radius = 76;
    painter->save();
    painter->setPen(innerScaleColor);

    painter->rotate(innerStartAngle);
    int steps = (innerScaleMajor * innerScaleMinor);
    double angleStep = (360.0 - innerStartAngle - innerEndAngle) / steps;
    QPen pen = painter->pen();
    pen.setCapStyle(Qt::RoundCap);

    for (int i = 0; i <= steps; i++) {
        if (i % innerScaleMinor == 0) {
            pen.setWidthF(1.5);
            painter->setPen(pen);
            painter->drawLine(0, radius - 12, 0, radius);
        } else {
            pen.setWidthF(1.0);
            painter->setPen(pen);
            painter->drawLine(0, radius - 5, 0, radius);
        }

        painter->rotate(angleStep);
    }

    painter->restore();
}

六、控件介紹

  1. 超過150個精美控件,涵蓋了各種儀表盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,flatui、高亮按鈕、滑動選擇器、農歷等。遠超qwt集成的控件數量。
  2. 每個類都可以獨立成一個單獨的控件,零耦合,每個控件一個頭文件和一個實現文件,不依賴其他文件,方便單個控件以源碼形式集成到項目中,較少代碼量。qwt的控件類環環相扣,高度耦合,想要使用其中一個控件,必須包含所有的代碼。
  3. 全部純Qt編寫,QWidget+QPainter繪制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等編譯器,支持任意操作系統比如windows+linux+mac+嵌入式linux等,不亂碼,可直接集成到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中拖曳設計使用。
  14. 目前已經有qml版本,后期會考慮出pyqt版本,如果用戶需求量很大的話。

七、SDK下載

  • SDK下載鏈接:https://pan.baidu.com/s/1A5Gd77kExm8Co5ckT51vvQ 提取碼:877p
  • 下載鏈接中包含了各個版本的動態庫文件,所有控件的頭文件,使用demo,自定義控件+屬性設計器。
  • 自定義控件插件開放動態庫dll使用(永久免費),無任何后門和限制,請放心使用。
  • 目前已提供26個版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
  • 不定期增加控件和完善控件,不定期更新SDK,歡迎各位提出建議,謝謝!
  • widget版本(QQ:517216493)qml版本(QQ:373955953)三峰駝(QQ:278969898)。
  • 濤哥的知乎專欄 Qt進階之路 https://zhuanlan.zhihu.com/TaoQt
  • 歡迎關注微信公眾號【高效程序員】,C++/Python、學習方法、寫作技巧、熱門技術、職場發展等內容,干貨多多,福利多多!
  • Qt入門書籍推薦霍亞飛的《Qt Creator快速入門》《Qt5編程入門》,Qt進階書籍推薦官方的《C++ GUI Qt4編程》。
  • 強烈推薦程序員自我修養和規划系列書《大話程序員》《程序員的成長課》《解憂程序員》!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM