天天看點

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

一、前言

天氣儀表盤控件是所有控件中唯一一個使用了svg矢量圖的控件,各種天氣圖示采用的矢量圖,顔色變換采用動态載入svg的内容更改生成的,其實也可以采用圖形字型來做,本次控件為了熟悉下svg在Qt中的使用,才采用的svg來繪制。

天氣一般要表示多個内容,溫度+濕度+天氣等,這就需要合理的布局多種元素的位置才能更加美觀一些,這裡參照的是網上一些通用的做法,比如最外層圓環是溫度,中間圓環濕度,然後天氣圖示貼在中間圓環裡邊的左上角,同時再繪制溫度濕度的值,動态變化的。

二、實作的功能

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

三、效果圖

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

四、頭檔案代碼

#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程式設計》。
  • 強烈推薦程式員自我修養和規劃系列書《大話程式員》《程式員的成長課》《解憂程式員》!

繼續閱讀