如今web形式的圖表框架很是流行,國產表明就是echart,本人用過幾回,三個字屌爆了來形容,很是強大,並且易用性也很是棒,仍是開源免費的,使用起來不要太爽,內置的各類圖表和儀表盤等很是豐富,展示形式也是很是多樣的。 本次要寫的圓弧進度條,就是參考自echart中的一個圓弧進度條,主要結構就是外圍一圈圓角進度,中間加上標題和對應進度的百分比,進度條的起始角度和結束角度能夠自行調整,這樣的話進度條的開口就能夠在左邊右邊上邊下邊等任意位置,經過調整角度就能實現。繪製的核心就是drawArc函數。linux
#ifndef PROGRESSARC_H #define PROGRESSARC_H /** * 百分比儀表盤控件 做者:feiyangqingyun(QQ:517216493) 2018-8-30 * 1:可設置範圍值,支持負數值 * 2:可設置精確度,最大支持小數點後3位 * 3:可設置圓弧寬度 * 4:可設置開始旋轉角度/結束旋轉角度 * 5:可設置儀表盤的標題 * 6:可設置背景顏色/進度顏色/值顏色/文字顏色 * 7:自適應窗體拉伸,文字自動縮放 * 8:可自由拓展各類漸變色 * 9:可設置百分比模式,自動計算值換算成百分比 */ #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 ProgressArc : public QWidget #else class ProgressArc : 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 startAngle READ getStartAngle WRITE setStartAngle) Q_PROPERTY(int endAngle READ getEndAngle WRITE setEndAngle) Q_PROPERTY(QColor arcColor READ getArcColor WRITE setArcColor) Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor) Q_PROPERTY(QColor titleColor READ getTitleColor WRITE setTitleColor) Q_PROPERTY(QColor baseColor READ getBaseColor WRITE setBaseColor) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor) Q_PROPERTY(bool percent READ getPercent WRITE setPercent) Q_PROPERTY(int arcWidth READ getArcWidth WRITE setArcWidth) Q_PROPERTY(QString title READ getTitle WRITE setTitle) public: explicit ProgressArc(QWidget *parent = 0); ~ProgressArc(); protected: void paintEvent(QPaintEvent *); void drawArc(QPainter *painter); void drawValue(QPainter *painter); void drawTitle(QPainter *painter); private: double minValue; //最小值 double maxValue; //最大值 double value; //目標值 int precision; //精確度,小數點後幾位 int startAngle; //開始旋轉角度 int endAngle; //結束旋轉角度 QColor arcColor; //圓弧顏色 QColor textColor; //文字顏色 QColor titleColor; //標題顏色 QColor baseColor; //基準顏色 QColor bgColor; //背景顏色 bool percent; //百分比模式 int arcWidth; //圓弧寬度 QString title; //標題 public: double getMinValue() const; double getMaxValue() const; double getValue() const; int getPrecision() const; int getStartAngle() const; int getEndAngle() const; QColor getArcColor() const; QColor getTextColor() const; QColor getTitleColor() const; QColor getBaseColor() const; QColor getBgColor() const; bool getPercent() const; int getArcWidth() const; QString getTitle() 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 setStartAngle(int startAngle); //設置結束旋轉角度 void setEndAngle(int endAngle); //設置圓弧顏色 void setArcColor(const QColor &arcColor); //設置文本顏色 void setTextColor(const QColor &textColor); //設置標題顏色 void setTitleColor(const QColor &titleColor); //設置基準顏色 void setBaseColor(const QColor &baseColor); //設置背景顏色 void setBgColor(const QColor &bgColor); //設置百分比模式 void setPercent(bool percent); //設置圓弧寬度 void setArcWidth(int arcWidth); //設置標題 void setTitle(const QString &title); Q_SIGNALS: void valueChanged(int value); }; #endif // PROGRESSARC_H
void ProgressArc::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); //繪製圓弧 drawArc(&painter); //繪製當前值 drawValue(&painter); //繪製標題 drawTitle(&painter); } void ProgressArc::drawArc(QPainter *painter) { int radius = 99 - arcWidth; painter->save(); painter->setBrush(Qt::NoBrush); QPen pen; pen.setWidthF(arcWidth); pen.setCapStyle(Qt::RoundCap); //計算總範圍角度,當前值範圍角度,剩餘值範圍角度 double angleAll = 360.0 - startAngle - endAngle; double angleCurrent = angleAll * ((value - minValue) / (maxValue - minValue)); double angleOther = angleAll - angleCurrent; QRectF rect = QRectF(-radius, -radius, radius * 2, radius * 2); //繪製圓弧背景 pen.setColor(baseColor); painter->setPen(pen); painter->drawArc(rect, (270 - startAngle - angleCurrent - angleOther) * 16, angleOther * 16); //繪製圓弧進度 pen.setColor(arcColor); painter->setPen(pen); painter->drawArc(rect, (270 - startAngle - angleCurrent) * 16, angleCurrent * 16); painter->restore(); } void ProgressArc::drawValue(QPainter *painter) { int radius = 100; painter->save(); painter->setPen(textColor); QFont font; font.setPixelSize(40); painter->setFont(font); QString strValue; if (percent) { double temp = value / (maxValue - minValue) * 100; strValue = QString("%1%").arg(temp, 0, 'f', precision); } else { strValue = QString("%1").arg((double)value, 0, 'f', precision); } QRectF textRect(-radius, 0, radius * 2, radius / 3); painter->drawText(textRect, Qt::AlignCenter, strValue); painter->restore(); } void ProgressArc::drawTitle(QPainter *painter) { double radius = 100; painter->save(); painter->setPen(titleColor); QFont font; font.setPixelSize(25); painter->setFont(font); QRectF textRect(-radius, -radius / 2.5, radius * 2, radius / 3); painter->drawText(textRect, Qt::AlignCenter, title); painter->restore(); }