前言
環形進度條,用來展示當前進度,為了滿足大屏UI的需要特意定制,以前有個叫圓環進度條,不能滿足項目需要,只能重新定做,以前的進度間距不能自適應分辨率,而且當前進度對應的反的進度不能單獨設置顏色,即當前進度90%,剩余的10%也需要設置成不同的顏色,還有一個重要的功能是,能夠指定多個警戒值,一旦超過或者小於該值,則當前進度自動切換到預先設定的警戒值顏色,而不需要用戶自己去判斷警戒值去設置警戒顏色,用戶只需要傳入當前值即可,這個功能非常實用,還可以設置警戒判斷的標准是超過值還是小於值報警。個人感覺這個環形進度條功能完爆市面上所有的圓環進度條。只要稍作參數設置可以變成各種想要的效果,什么起始角度+動畫效果+順時針逆時針轉等。
實現的功能
- 1:可設置范圍值,支持負數值
- 2:可設置精確度,最大支持小數點后3位
- 3:可設置起始角度
- 4:可設置三種值+三種顏色,啟用自動檢測值后繪制不同的顏色
- 5:可設置是否啟用動畫效果以及動畫效果每次移動的步長
- 6:可設置背景顏色/文字顏色/進度顏色/中間圓顏色
- 7:可設置值警戒報警比較模式 0-不比較 1-最大值報警 2-最小值報警
- 8:可設置顯示的值是百分比
- 9:可設置圓環與背景之間的距離即間距
- 10:可設置圓環的寬度
- 11:可設置圓環背景顏色,形成兩種顏色差
- 12:可設置順時針逆時針轉
- 13:自適應窗體拉伸,刻度尺和文字自動縮放
效果圖
頭文件代碼
#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();
}
控件介紹
- 超過145個精美控件,涵蓋了各種儀表盤、進度條、進度球、指南針、曲線圖、標尺、溫度計、導航條、導航欄,flatui、高亮按鈕、滑動選擇器、農歷等。遠超qwt集成的控件數量。
- 每個類都可以獨立成一個單獨的控件,零耦合,每個控件一個頭文件和一個實現文件,不依賴其他文件,方便單個控件以源碼形式集成到項目中,較少代碼量。qwt的控件類環環相扣,高度耦合,想要使用其中一個控件,必須包含所有的代碼。
- 全部純Qt編寫,QWidget+QPainter繪制,支持Qt4.6到Qt5.12的任何Qt版本,支持mingw、msvc、gcc等編譯器,不亂碼,可直接集成到Qt Creator中,和自帶的控件一樣使用,大部分效果只要設置幾個屬性即可,極為方便。
- 每個控件都有一個對應的單獨的包含該控件源碼的DEMO,方便參考使用。同時還提供一個所有控件使用的集成的DEMO。
- 每個控件的源代碼都有詳細中文注釋,都按照統一設計規范編寫,方便學習自定義控件的編寫。
- 每個控件默認配色和demo對應的配色都非常精美。
- 超過130個可見控件,6個不可見控件。
- 部分控件提供多種樣式風格選擇,多種指示器樣式選擇。
- 所有控件自適應窗體拉伸變化。
- 集成自定義控件屬性設計器,支持拖曳設計,所見即所得,支持導入導出xml格式。
- 自帶activex控件demo,所有控件可以直接運行在ie瀏覽器中。
- 集成fontawesome圖形字體+阿里巴巴iconfont收藏的幾百個圖形字體,享受圖形字體帶來的樂趣。
- 所有控件最后生成一個dll動態庫文件,可以直接集成到qtcreator中拖曳設計使用。
SDK下載
- SDK下載鏈接:https://pan.baidu.com/s/1tD9v1YPfE2fgYoK6lqUr1Q 提取碼:lyhk
- 自定義控件+屬性設計器欣賞:https://pan.baidu.com/s/1l6L3rKSiLu_uYi7lnL3ibQ 提取碼:tmvl
- 下載鏈接中包含了各個版本的動態庫文件,所有控件的頭文件,使用demo。
- 自定義控件插件開放動態庫dll使用(永久免費),無任何后門和限制,請放心使用。
- 目前已提供22個版本的dll,其中包括了qt5.12.3 msvc2017 32+64 mingw 32+64 的。
- 不定期增加控件和完善控件,不定期更新SDK,歡迎各位提出建議,謝謝!
- widget版本(QQ:517216493)qml版本(QQ:373955953)三峰駝(QQ:278969898)。