在Qt自定義控件中,儀表盤控件是數量最多的,寫儀表盤都寫到快要吐血,多是由於各類工業控制領域用的比較多吧,並且儀表盤又是比較生動直觀的,此次看到百度的echart中有這個控件,因此也來模仿作了一個,其實掌握了一兩個儀表盤的繪製方法之後,其餘儀表盤的繪製都是如魚得水,基本上變化很小。總結起來就以下幾點: 1:儀表盤邊框 2:刻度尺 3:刻度值 4:圓環進度 5:指針 6:當前值 7:儀表盤標題 不管什麼儀表盤,基本上包含的上面幾個要素的大部分,因此只要掌握幾個要素的繪製,任何儀表盤繪製都是輕輕鬆鬆信手拈來。無非就是有些儀表盤要求刻度尺在裏邊有些要求在外邊,有些要求有圓環進度不一樣顏色顯示,有些要求能夠自定義左側起始角度和右側結束角度,有些要求指針圓形方形橢圓形等,有些要求值改變的時候帶一些緩慢的動畫過渡效果等。linux
#ifndef GAUGEPANEL_H #define GAUGEPANEL_H /** * 面板儀表盤控件 做者:feiyangqingyun(QQ:517216493) 2019-7-3 * 1:可設置範圍值,支持負數值 * 2:可設置精確度+刻度尺精確度,最大支持小數點後3位 * 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 GaugePanel : public QWidget #else class GaugePanel : 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(int scalePrecision READ getScalePrecision WRITE setScalePrecision) Q_PROPERTY(int scaleMajor READ getScaleMajor WRITE setScaleMajor) Q_PROPERTY(int scaleMinor READ getScaleMinor WRITE setScaleMinor) Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle) Q_PROPERTY(int endAngle READ getEndAngle WRITE setEndAngle) Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation) Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep) Q_PROPERTY(int ringWidth READ getRingWidth WRITE setRingWidth) Q_PROPERTY(QColor ringColor READ getRingColor WRITE setRingColor) Q_PROPERTY(QColor scaleColor READ getScaleColor WRITE setScaleColor) Q_PROPERTY(QColor pointerColor READ getPointerColor WRITE setPointerColor) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor) Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) Q_PROPERTY(QString unit READ getUnit WRITE setUnit) Q_PROPERTY(QString text READ getText WRITE setText) public: explicit GaugePanel(QWidget *parent = 0); ~GaugePanel(); protected: void paintEvent(QPaintEvent *); void drawRing(QPainter *painter); void drawScale(QPainter *painter); void drawScaleNum(QPainter *painter); void drawPointer(QPainter *painter); void drawValue(QPainter *painter); private slots: void updateValue(); private: double minValue; //最小值 double maxValue; //最大值 double value; //目標值 int precision; //精確度,小數點後幾位 int scalePrecision; //刻度尺精確度,小數點後幾位 int scaleMajor; //大刻度數量 int scaleMinor; //小刻度數量 int startAngle; //開始旋轉角度 int endAngle; //結束旋轉角度 bool animation; //是否啓用動畫顯示 double animationStep; //動畫顯示時步長 int ringWidth; //圓環寬度 QColor ringColor; //圓環顏色 QColor scaleColor; //刻度顏色 QColor pointerColor; //指針顏色 QColor bgColor; //背景顏色 QColor textColor; //文字顏色 QString unit; //單位 QString text; //描述文字 bool reverse; //是否往回走 double currentValue; //當前值 QTimer *timer; //定時器繪製動畫 public: double getMinValue() const; double getMaxValue() const; double getValue() const; int getPrecision() const; int getScalePrecision() const; int getScaleMajor() const; int getScaleMinor() const; int getStartAngle() const; int getEndAngle() const; bool getAnimation() const; double getAnimationStep() const; int getRingWidth() const; QColor getRingColor() const; QColor getScaleColor() const; QColor getPointerColor() const; QColor getBgColor() const; QColor getTextColor() const; QString getUnit() const; QString getText() 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 setScalePrecision(int scalePrecision); //設置主刻度數量 void setScaleMajor(int scaleMajor); //設置小刻度數量 void setScaleMinor(int scaleMinor); //設置開始旋轉角度 void setStartAngle(int startAngle); //設置結束旋轉角度 void setEndAngle(int endAngle); //設置是否啓用動畫顯示 void setAnimation(bool animation); //設置動畫顯示的步長 void setAnimationStep(double animationStep); //設置圓環寬度+顏色 void setRingWidth(int ringWidth); void setRingColor(const QColor &ringColor); //設置刻度顏色 void setScaleColor(const QColor &scaleColor); //設置指針顏色 void setPointerColor(const QColor &pointerColor); //設置背景顏色 void setBgColor(const QColor &bgColor); //設置文本顏色 void setTextColor(const QColor &textColor); //設置單位 void setUnit(const QString &unit); //設置中間描述文字 void setText(const QString &text); Q_SIGNALS: void valueChanged(int value); }; #endif // GAUGEPANEL_H
void GaugePanel::paintEvent(QPaintEvent *) { int width = this->width(); int height = this->height(); int side = qMin(width, height); //繪製準備工做,啓用反鋸齒,平移座標軸中心,等比例縮放 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //繪製背景 if (bgColor != Qt::transparent) { painter.setPen(Qt::NoPen); painter.fillRect(this->rect(), bgColor); } painter.translate(width / 2, height / 2); painter.scale(side / 200.0, side / 200.0); //繪製圓環 drawRing(&painter); //繪製刻度線 drawScale(&painter); //繪製刻度值 drawScaleNum(&painter); //繪製指示器 drawPointer(&painter); //繪製當前值 drawValue(&painter); } void GaugePanel::drawRing(QPainter *painter) { int radius = 70; painter->save(); QPen pen; pen.setCapStyle(Qt::FlatCap); pen.setWidthF(ringWidth); pen.setColor(ringColor); painter->setPen(pen); radius = radius - ringWidth; QRectF rect = QRectF(-radius, -radius, radius * 2, radius * 2); double angleAll = 360.0 - startAngle - endAngle; painter->drawArc(rect, (270 - startAngle - angleAll) * 16, angleAll * 16); painter->restore(); } void GaugePanel::drawScale(QPainter *painter) { int radius = 80; painter->save(); painter->rotate(startAngle); int steps = (scaleMajor * scaleMinor); double angleStep = (360.0 - startAngle - endAngle) / steps; QPen pen; pen.setCapStyle(Qt::RoundCap); pen.setColor(scaleColor); for (int i = 0; i <= steps; i++) { if (i % scaleMinor == 0) { pen.setWidthF(1.5); painter->setPen(pen); painter->drawLine(0, radius - 8, 0, radius + 5); } else { pen.setWidthF(0.5); painter->setPen(pen); painter->drawLine(0, radius - 8, 0, radius - 3); } painter->rotate(angleStep); } painter->restore(); } void GaugePanel::drawScaleNum(QPainter *painter) { int radius = 95; painter->save(); painter->setPen(scaleColor); double startRad = (360 - startAngle - 90) * (M_PI / 180); double deltaRad = (360 - startAngle - endAngle) * (M_PI / 180) / scaleMajor; for (int i = 0; i <= scaleMajor; i++) { double sina = qSin(startRad - i * deltaRad); double cosa = qCos(startRad - i * deltaRad); double value = 1.0 * i * ((maxValue - minValue) / scaleMajor) + minValue; QString strValue = QString("%1").arg((double)value, 0, 'f', scalePrecision); double textWidth = fontMetrics().width(strValue); double textHeight = fontMetrics().height(); int x = radius * cosa - textWidth / 2; int y = -radius * sina + textHeight / 4; painter->drawText(x, y, strValue); } painter->restore(); } void GaugePanel::drawPointer(QPainter *painter) { int radius = 70; painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(pointerColor); QPolygon pts; pts.setPoints(4, -5, 0, 0, -8, 5, 0, 0, radius); painter->rotate(startAngle); double degRotate = (360.0 - startAngle - endAngle) / (maxValue - minValue) * (currentValue - minValue); painter->rotate(degRotate); painter->drawConvexPolygon(pts); painter->restore(); } void GaugePanel::drawValue(QPainter *painter) { int radius = 100; painter->save(); painter->setPen(textColor); QFont font; font.setPixelSize(15); painter->setFont(font); QString strValue = QString("%1").arg((double)currentValue, 0, 'f', precision); strValue = QString("%1 %2").arg(strValue).arg(unit); QRectF valueRect(-radius, radius / 3.5, radius * 2, radius / 3.5); painter->drawText(valueRect, Qt::AlignCenter, strValue); QRectF textRect(-radius, radius / 2.5, radius * 2, radius / 2.5); font.setPixelSize(12); painter->setFont(font); painter->drawText(textRect, Qt::AlignCenter, text); painter->restore(); }