Qt編寫自定義控件6-指南針儀表盤

前言

指南針儀表盤,主要用來指示東南西北四個方位,雙向對稱兩個指針旋轉,其實就是360度打轉,功能屬於簡單型,可能指針的繪製稍微難一點,須要計算多個點構成多邊形,本系列控件文章將會連續發100+篇,一方面爲了鍛鍊本身的毅力+堅持力,一方面爲了宣傳本身,若是各位對完整的源碼有興趣能夠私聊,也歡迎在文章下面評論提出建議,謝謝!瀏覽器

實現的功能

  • 1:可設置當前度數
  • 2:可設置精確度
  • 3:可設置是否啓用動畫及步長
  • 4:可設置邊框漸變顏色
  • 5:可設置背景漸變顏色
  • 6:可設置加深和明亮顏色
  • 7:可設置指南指北指針顏色
  • 8:可設置中心點漸變顏色

效果圖

頭文件代碼

#ifndef GAUGECOMPASS_H
#define GAUGECOMPASS_H

/**
 * 指南針儀表盤控件 做者:feiyangqingyun(QQ:517216493) 2016-11-12
 * 1:可設置當前度數
 * 2:可設置精確度
 * 3:可設置是否啓用動畫及步長
 * 4:可設置邊框漸變顏色
 * 5:可設置背景漸變顏色
 * 6:可設置加深和明亮顏色
 * 7:可設置指南指北指針顏色
 * 8:可設置中心點漸變顏色
 */

#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 GaugeCompass : public QWidget
#else
class GaugeCompass : public QWidget
#endif

{
    Q_OBJECT
    Q_PROPERTY(double value READ getValue WRITE setValue)
    Q_PROPERTY(int precision READ getPrecision WRITE setPrecision)

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

    Q_PROPERTY(QColor crownColorStart READ getCrownColorStart WRITE setCrownColorStart)
    Q_PROPERTY(QColor crownColorEnd READ getCrownColorEnd WRITE setCrownColorEnd)

    Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart)
    Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd)

    Q_PROPERTY(QColor darkColor READ getDarkColor WRITE setDarkColor)
    Q_PROPERTY(QColor lightColor READ getLightColor WRITE setLightColor)

    Q_PROPERTY(QColor foreground READ getForeground WRITE setForeground)
    Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)

    Q_PROPERTY(QColor northPointerColor READ getNorthPointerColor WRITE setNorthPointerColor)
    Q_PROPERTY(QColor southPointerColor READ getSouthPointerColor WRITE setSouthPointerColor)

    Q_PROPERTY(QColor centerColorStart READ getCenterColorStart WRITE setCenterColorStart)
    Q_PROPERTY(QColor centerColorEnd READ getCenterColorEnd WRITE setCenterColorEnd)

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

protected:
    void paintEvent(QPaintEvent *);
    void drawCrownCircle(QPainter *painter);
    void drawBgCircle(QPainter *painter);
    void drawScale(QPainter *painter);
    void drawScaleNum(QPainter *painter);
    void drawCoverOuterCircle(QPainter *painter);
    void drawCoverInnerCircle(QPainter *painter);
    void drawCoverCenterCircle(QPainter *painter);
    void drawPointer(QPainter *painter);
    void drawCenterCircle(QPainter *painter);
    void drawValue(QPainter *painter);

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

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

    QColor crownColorStart;         //外邊框漸變開始顏色
    QColor crownColorEnd;           //外邊框漸變結束顏色

    QColor bgColorStart;            //背景漸變開始顏色
    QColor bgColorEnd;              //背景漸變結束顏色

    QColor darkColor;               //加深顏色
    QColor lightColor;              //明亮顏色

    QColor foreground;              //前景色
    QColor textColor;               //文字顏色

    QColor northPointerColor;       //北方指針顏色
    QColor southPointerColor;       //南方指針顏色

    QColor centerColorStart;        //中心圓漸變開始顏色
    QColor centerColorEnd;          //中心圓漸變結束顏色

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

private slots:
    void updateValue();

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

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

    QColor getCrownColorStart()     const;
    QColor getCrownColorEnd()       const;

    QColor getBgColorStart()        const;
    QColor getBgColorEnd()          const;

    QColor getDarkColor()           const;
    QColor getLightColor()          const;

    QColor getForeground()          const;
    QColor getTextColor()           const;

    QColor getNorthPointerColor()   const;
    QColor getSouthPointerColor()   const;

    QColor getCenterColorStart()    const;
    QColor getCenterColorEnd()      const;

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

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

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

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

    //設置外邊框漸變顏色
    void setCrownColorStart(const QColor &crownColorStart);
    void setCrownColorEnd(const QColor &crownColorEnd);

    //設置背景漸變顏色
    void setBgColorStart(const QColor &bgColorStart);
    void setBgColorEnd(const QColor &bgColorEnd);

    //設置加深和明亮顏色
    void setDarkColor(const QColor &darkColor);
    void setLightColor(const QColor &lightColor);

    //設置前景色
    void setForeground(const QColor &foreground);
    //設置文本顏色
    void setTextColor(const QColor &textColor);

    //設置指針顏色
    void setNorthPointerColor(const QColor &northPointerColor);
    void setSouthPointerColor(const QColor &southPointerColor);

    //設置中心圓顏色
    void setCenterColorStart(const QColor &centerColorStart);
    void setCenterColorEnd(const QColor &centerColorEnd);

Q_SIGNALS:
    void valueChanged(int value);
};

#endif // GAUGECOMPASS_H

核心代碼

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

    //繪製外邊框圓
    drawCrownCircle(&painter);
    //繪製背景圓
    drawBgCircle(&painter);
    //繪製刻度
    drawScale(&painter);
    //繪製東南西北標識
    drawScaleNum(&painter);
    //繪製覆蓋圓外圓
    drawCoverOuterCircle(&painter);
    //繪製覆蓋圓內圓
    drawCoverInnerCircle(&painter);
    //繪製覆蓋圓中心圓
    drawCoverCenterCircle(&painter);
    //繪製南北指針
    drawPointer(&painter);
    //繪製中心圓
    drawCenterCircle(&painter);
    //繪製當前值
    drawValue(&painter);
}

void GaugeCompass::drawCrownCircle(QPainter *painter)
{
    int radius = 99;
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient lineGradient(0, -radius, 0, radius);
    lineGradient.setColorAt(0, crownColorStart);
    lineGradient.setColorAt(1, crownColorEnd);
    painter->setBrush(lineGradient);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void GaugeCompass::drawBgCircle(QPainter *painter)
{
    int radius = 90;
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient lineGradient(0, -radius, 0, radius);
    lineGradient.setColorAt(0, bgColorStart);
    lineGradient.setColorAt(1, bgColorEnd);
    painter->setBrush(lineGradient);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void GaugeCompass::drawScale(QPainter *painter)
{
    int radius = 85;
    painter->save();

    //總共8格,4格爲NESW字母,4格爲線條
    int steps = 8;
    double angleStep = 360.0 / steps;

    QPen pen;
    pen.setColor(foreground);
    pen.setCapStyle(Qt::RoundCap);
    pen.setWidth(4);    
    painter->setPen(pen);

    //%2整數部分繪製NESW字母,其他繪製線條刻度
    for (int i = 0; i <= steps; i++) {
        if (i % 2 != 0) {
            painter->drawLine(0, radius - 10, 0, radius);
        }

        painter->rotate(angleStep);
    }

    painter->restore();
}

void GaugeCompass::drawScaleNum(QPainter *painter)
{
    int radius = 88;
    painter->save();
    painter->setPen(foreground);

    QFont font;
    font.setPixelSize(15);
    font.setBold(true);
    painter->setFont(font);

    QRect textRect = QRect(-radius, -radius, radius * 2, radius * 2);
    painter->drawText(textRect, Qt::AlignTop | Qt::AlignHCenter, "N");
    painter->drawText(textRect, Qt::AlignBottom | Qt::AlignHCenter, "S");

    radius -= 2;
    textRect = QRect(-radius, -radius, radius * 2, radius * 2);
    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, "W");

    radius -= 2;
    textRect = QRect(-radius, -radius, radius * 2, radius * 2);
    painter->drawText(textRect, Qt::AlignRight | Qt::AlignVCenter, "E");

    painter->restore();
}

void GaugeCompass::drawCoverOuterCircle(QPainter *painter)
{
    int radius = 68;
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient lineGradient(0, -radius, 0, radius);
    lineGradient.setColorAt(0, lightColor);
    lineGradient.setColorAt(1, darkColor);
    painter->setBrush(lineGradient);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void GaugeCompass::drawCoverInnerCircle(QPainter *painter)
{
    int radius = 60;
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient lineGradient(0, -radius, 0, radius);
    lineGradient.setColorAt(0, darkColor);
    lineGradient.setColorAt(1, lightColor);
    painter->setBrush(lineGradient);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void GaugeCompass::drawCoverCenterCircle(QPainter *painter)
{
    int radius = 15;
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setOpacity(0.8);
    QLinearGradient lineGradient(0, -radius, 0, radius);
    lineGradient.setColorAt(0, lightColor);
    lineGradient.setColorAt(1, darkColor);
    painter->setBrush(lineGradient);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void GaugeCompass::drawPointer(QPainter *painter)
{
    int radius = 75;

    QPolygon pts;

    painter->save();
    painter->setOpacity(0.7);
    painter->setPen(Qt::NoPen);
    painter->setBrush(northPointerColor);
    pts.setPoints(3, -10, 0, 10, 0, 0, radius);
    painter->rotate(currentValue + 180);
    painter->drawConvexPolygon(pts);
    painter->restore();

    painter->save();
    painter->setOpacity(0.7);
    painter->setPen(Qt::NoPen);
    painter->setBrush(southPointerColor);
    pts.setPoints(3, -10, 0, 10, 0, 0, radius);
    painter->rotate(currentValue);
    painter->drawConvexPolygon(pts);
    painter->restore();
}

void GaugeCompass::drawCenterCircle(QPainter *painter)
{
    int radius = 12;
    painter->save();
    painter->setOpacity(1.0);
    painter->setPen(Qt::NoPen);
    QLinearGradient lineGradient(0, -radius, 0, radius);
    lineGradient.setColorAt(0, centerColorStart);
    lineGradient.setColorAt(1, centerColorEnd);
    painter->setBrush(lineGradient);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
    painter->restore();
}

void GaugeCompass::drawValue(QPainter *painter)
{
    int radius = 100;
    painter->save();
    painter->setPen(textColor);

    QFont font;
    font.setPixelSize(11);
    font.setBold(true);
    painter->setFont(font);

    QRectF textRect(-radius, -radius, radius * 2, radius * 2);
    QString strValue = QString("%1").arg((double)value, 0, 'f', precision);
    painter->drawText(textRect, Qt::AlignCenter, strValue);
    painter->restore();
}

控件介紹

  1. 超過130個精美控件,涵蓋了各類儀表盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,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. 超過120個可見控件,6個不可見控件。
  8. 部分控件提供多種樣式風格選擇,多種指示器樣式選擇。
  9. 全部控件自適應窗體拉伸變化。
  10. 集成自定義控件屬性設計器,支持拖曳設計,所見即所得,支持導入導出xml格式。
  11. 自帶activex控件demo,全部控件能夠直接運行在ie瀏覽器中。
  12. 集成fontawesome圖形字體+阿里巴巴iconfont收藏的幾百個圖形字體,享受圖形字體帶來的樂趣。
  13. 全部控件最後生成一個dll動態庫文件,能夠直接集成到qtcreator中拖曳設計使用。

SDK下載


相關文章
相關標籤/搜索