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