環形進度條,用來展現當前進度,爲了知足大屏UI的須要特地定製,之前有個叫圓環進度條,不能知足項目須要,只能從新定作,之前的進度間距不能自適應分辨率,並且當前進度對應的反的進度不能單獨設置顏色,即當前進度90%,剩餘的10%也須要設置成不一樣的顏色,還有一個重要的功能是,可以指定多個警惕值,一旦超過或者小於該值,則當前進度自動切換到預先設定的警惕值顏色,而不須要用戶本身去判斷警惕值去設置警惕顏色,用戶只須要傳入當前值便可,這個功能很是實用,還能夠設置警惕判斷的標準是超過值仍是小於值報警。我的感受這個環形進度條功能完爆市面上全部的圓環進度條。只要稍做參數設置能夠變成各類想要的效果,什麼起始角度+動畫效果+順時針逆時針轉等。c++
#ifndef PROGRESSRING_H #define PROGRESSRING_H /** * 環形進度條控件 做者:feiyangqingyun(QQ:517216493) 2019-5-1 * 1:可設置範圍值,支持負數值 * 2:可設置精確度,最大支持小數點後3位 * 3:可設置起始角度 * 4:可設置三種值+三種顏色,啓用自動檢測值後繪製不一樣的顏色 * 5:可設置是否啓用動畫效果以及動畫效果每次移動的步長 * 6:可設置背景顏色/文字顏色/進度顏色/中間圓顏色 * 7:可設置值警惕報警比較模式 0-不比較 1-最大值報警 2-最小值報警 * 8:可設置顯示的值是百分比 * 9:可設置圓環與背景之間的距離即間距 * 10:可設置圓環的寬度 * 11:可設置圓環背景顏色,造成兩種顏色差 * 12:可設置順時針逆時針轉 * 13:自適應窗體拉伸,刻度尺和文字自動縮放 */ #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 ProgressRing : public QWidget #else class ProgressRing : 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(bool showPercent READ getShowPercent WRITE setShowPercent) Q_PROPERTY(int alarmMode READ getAlarmMode WRITE setAlarmMode) Q_PROPERTY(int startAngle READ getStartAngle WRITE setStartAngle) Q_PROPERTY(int ringPadding READ getRingPadding WRITE setRingPadding) Q_PROPERTY(int ringWidth READ getRingWidth WRITE setRingWidth) Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation) Q_PROPERTY(double animationStep READ getAnimationStep WRITE setAnimationStep) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor) Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) Q_PROPERTY(QColor ringColor READ getRingColor WRITE setRingColor) Q_PROPERTY(QColor ringBgColor READ getRingBgColor WRITE setRingBgColor) Q_PROPERTY(QColor circleColor READ getCircleColor WRITE setCircleColor) Q_PROPERTY(int ringValue1 READ getRingValue1 WRITE setRingValue1) Q_PROPERTY(int ringValue2 READ getRingValue2 WRITE setRingValue2) Q_PROPERTY(int ringValue3 READ getRingValue3 WRITE setRingValue3) Q_PROPERTY(QColor ringColor1 READ getRingColor1 WRITE setRingColor1) Q_PROPERTY(QColor ringColor2 READ getRingColor2 WRITE setRingColor2) Q_PROPERTY(QColor ringColor3 READ getRingColor3 WRITE setRingColor3) public: explicit ProgressRing(QWidget *parent = 0); ~ProgressRing(); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawRing(QPainter *painter); void drawPadding(QPainter *painter); void drawCircle(QPainter *painter); void drawValue(QPainter *painter); private slots: void updateValue(); private: double minValue; //最小值 double maxValue; //最大值 double value; //目標值 int precision; //精確度,小數點後幾位 bool clockWise; //順時針逆時針 bool showPercent; //顯示百分比 int alarmMode; //警惕報警模式,進度爲不一樣的顏色 int startAngle; //起始角度 int ringPadding; //圓環間距 int ringWidth; //圓環寬度 bool animation; //是否啓用動畫顯示 double animationStep; //動畫顯示時步長 QColor bgColor; //背景顏色 QColor textColor; //文字顏色 QColor ringColor; //圓環顏色 QColor ringBgColor; //圓環進度背景 QColor circleColor; //中心圓顏色 int ringValue1; //環形值1 int ringValue2; //環形值2 int ringValue3; //環形值3 QColor ringColor1; //環形顏色1 QColor ringColor2; //環形顏色2 QColor ringColor3; //環形顏色3 bool reverse; //是否往回走 double currentValue; //當前值 QTimer *timer; //定時器繪製動畫 public: double getMinValue() const; double getMaxValue() const; double getValue() const; int getPrecision() const; bool getClockWise() const; bool getShowPercent() const; int getAlarmMode() const; int getStartAngle() const; int getRingPadding() const; int getRingWidth() const; bool getAnimation() const; double getAnimationStep() const; QColor getBgColor() const; QColor getTextColor() const; QColor getRingColor() const; QColor getRingBgColor() const; QColor getCircleColor() const; int getRingValue1() const; int getRingValue2() const; int getRingValue3() const; QColor getRingColor1() const; QColor getRingColor2() const; QColor getRingColor3() 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 setClockWise(bool clockWise); //設置顯示百分比 void setShowPercent(bool showPercent); //設置啓動自動檢驗 void setAlarmMode(int alarmMode); //設置起始角度 void setStartAngle(int startAngle); //設置圓環間距 void setRingPadding(int ringPadding); //設置圓環寬度 void setRingWidth(int ringWidth); //設置是否啓用動畫顯示 void setAnimation(bool animation); //設置動畫顯示的步長 void setAnimationStep(double animationStep); //設置背景顏色 void setBgColor(const QColor &bgColor); //設置文本顏色 void setTextColor(const QColor &textColor); //設置圓環進度顏色 void setRingColor(const QColor &ringColor); //設置圓環背景顏色 void setRingBgColor(const QColor &ringBgColor); //設置中心圓顏色 void setCircleColor(const QColor &circleColor); //設置三種值 void setRingValue1(int ringValue1); void setRingValue2(int ringValue2); void setRingValue3(int ringValue3); //設置三種顏色 void setRingColor1(const QColor &ringColor1); void setRingColor2(const QColor &ringColor2); void setRingColor3(const QColor &ringColor3); Q_SIGNALS: void valueChanged(int value); }; #endif // PROGRESSRING_H
void ProgressRing::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); //繪製背景 drawBg(&painter); //繪製進度 drawRing(&painter); //繪製間隔,從新繪製一個圓遮住,產生間距效果 if (ringPadding > 0) { drawPadding(&painter); } //繪製中間圓 drawCircle(&painter); //繪製當前值 drawValue(&painter); } void ProgressRing::drawBg(QPainter *painter) { int radius = 99; painter->save(); painter->setPen(Qt::NoPen); //這裏有個技巧,若是沒有間距則設置成圓環的背景色 painter->setBrush(ringPadding == 0 ? ringBgColor : bgColor); painter->drawEllipse(-radius, -radius, radius * 2, radius * 2); painter->restore(); } void ProgressRing::drawRing(QPainter *painter) { int radius = 99 - ringPadding; painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(ringColor); QRectF rect(-radius, -radius, radius * 2, radius * 2); //計算總範圍角度,當前值範圍角度,剩餘值範圍角度 double angleAll = 360.0; double angleCurrent = angleAll * ((currentValue - minValue) / (maxValue - minValue)); double angleOther = angleAll - angleCurrent; //若是逆時針 if (!clockWise) { angleCurrent = -angleCurrent; angleOther = -angleOther; } //動態設置當前進度顏色 QColor color = ringColor; if (alarmMode == 1) { if (currentValue >= ringValue3) { color = ringColor3; } else if (currentValue >= ringValue2) { color = ringColor2; } else { color = ringColor1; } } else if (alarmMode == 2) { if (currentValue <= ringValue1) { color = ringColor1; } else if (currentValue <= ringValue2) { color = ringColor2; } else { color = ringColor3; } } //繪製當前值餅圓 painter->setBrush(color); painter->drawPie(rect, (startAngle - angleCurrent) * 16, angleCurrent * 16); //繪製剩餘值餅圓 painter->setBrush(ringBgColor); painter->drawPie(rect, (startAngle - angleCurrent - angleOther) * 16, angleOther * 16); painter->restore(); } void ProgressRing::drawPadding(QPainter *painter) { int radius = 99 - ringWidth - ringPadding; painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(bgColor); painter->drawEllipse(-radius, -radius, radius * 2, radius * 2); painter->restore(); } void ProgressRing::drawCircle(QPainter *painter) { //文字的區域要減去進度的寬度及間距 int radius = 99 - ringWidth - (ringPadding * 2); painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(circleColor); painter->drawEllipse(-radius, -radius, radius * 2, radius * 2); painter->restore(); } void ProgressRing::drawValue(QPainter *painter) { //文字的區域要減去進度的寬度及間距 int radius = 99 - ringWidth - (ringPadding * 2); painter->save(); painter->setPen(textColor); QFont font; int fontSize = radius - (showPercent ? 20 : 6); font.setPixelSize(fontSize); painter->setFont(font); QRectF textRect(-radius, -radius, radius * 2, radius * 2); QString strValue; if (showPercent) { double percent = (currentValue * 100) / (maxValue - minValue); strValue = QString("%1%").arg(percent, 0, 'f', precision); } else { strValue = QString("%1").arg(currentValue, 0, 'f', precision); } painter->drawText(textRect, Qt::AlignCenter, strValue); painter->restore(); }