【Qt】圆形进度条

[toc]

实现效果

h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _QROUNDPROGRESSBAR_H_
#define _QROUNDPROGRESSBAR_H_

#include <QWidget>

class QRoundProgressBar : public QWidget
{
Q_OBJECT
public:
//进度条形状
enum BarStyle
{
//圆环状
StyleDonut = 0,
//圆饼状
StylePie,
//圆线状
StyleLine
};


public:
QRoundProgressBar(QWidget *parent , BarStyle style = StyleLine);
~QRoundProgressBar();

public:
//设置扫描弧度起始角度
void setStartAngle(double position);

//设置轮廓画笔的宽度
void setOutlinePenWidth(double penWidth);

//设置画笔宽度
void setDataPenWidth(double penWidth);

//设置进度条精度 88.88%,几位小数
void setDecimals(int count);

//设置进度条风格
void setBarStyle(BarStyle style);

//设置范围
void setRange(double min, double max);

//设置当前值
void setValue(int val);

//设置当前值
void setValue(double val);

//设置最小值
void setMinimum(double min);

//设置最大值
void setMaximum(double max);

protected:
void paintEvent(QPaintEvent *event);

//画基础图形
void drawBase(QPainter& p, const QRectF& baseRect, const QRectF &innerRect);

//根据值画出进度条
void drawValue(QPainter& p, const QRectF& baseRect, double value, double arcLength, const QRectF & innerRect
, double innerDiameter);

//画中心文字
void drawText(QPainter& p, const QRectF& innerRect, double value);

private:
double m_min; //最小值
double m_max; //最大值
double m_value; //当前值
BarStyle m_barStyle; //进度条风格
int m_decimals; //进度条精度
double m_startAngel; //扫描弧度起始角度
double m_outlinePenWidth; //轮廓画笔的宽度
double m_dataPenWidth; //画笔宽度
};

#endif // _QROUNDPROGRESSBAR_H_

cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "QRoundProgressBar.h"
#include <QPainter>
#include <QPointF>
#include <QtMath>

QRoundProgressBar::QRoundProgressBar(QWidget *parent , BarStyle style)
: QWidget(parent)
, m_min(0)
, m_max(100)
, m_value(0)
, m_startAngel(90)
, m_barStyle(style)
, m_outlinePenWidth(0)
, m_dataPenWidth(0)
, m_decimals(0)
{
}

QRoundProgressBar::~QRoundProgressBar()
{
}


void QRoundProgressBar::setStartAngle(double angle)
{
if (angle != m_startAngel)
{
m_startAngel = angle;
update();
}
}

void QRoundProgressBar::setOutlinePenWidth(double penWidth)
{
if (penWidth != m_outlinePenWidth)
{
m_outlinePenWidth = penWidth;
update();
}
}


void QRoundProgressBar::setDataPenWidth(double penWidth)
{
if (penWidth != m_dataPenWidth)
{
m_dataPenWidth = penWidth;
update();
}
}



void QRoundProgressBar::setDecimals(int count)
{
if (count >= 0 && count != m_decimals)
{
m_decimals = count;
update();
}
}


void QRoundProgressBar::setBarStyle(BarStyle style)
{
if (style != m_barStyle)
{
m_barStyle = style;
update();
}
}


void QRoundProgressBar::setRange(double min, double max)
{
m_min = min;
m_max = max;

if (m_max < m_min)
{
qSwap(m_max, m_min);
}

if (m_value < m_min)
{
m_value = m_min;
}
else if (m_value > m_max)
{
m_value = m_max;
}

update();
}

void QRoundProgressBar::setValue(double val)
{
if (m_value != val)
{
if (val < m_min)
{
m_value = m_min;
}
else if (val > m_max)
{
m_value = m_max;
}
else
{
m_value = val;
}
update();
}
}

void QRoundProgressBar::setValue(int val)
{
setValue(double(val));
}


void QRoundProgressBar::setMinimum(double min)
{
setRange(min, m_max);
}

void QRoundProgressBar::setMaximum(double max)
{
setRange(m_min, max);
}


void QRoundProgressBar::paintEvent(QPaintEvent* /*event*/)
{
//外圈直径
double outerDiameter = this->width();

//外圈矩形
QRectF baseRect(0, 0, outerDiameter, outerDiameter);

QPainter p(this);
p.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

//画背景矩形填充白色
p.fillRect(baseRect, QBrush(QColor("#FFFFFF")));

//内圆直径
double innerDiameter = width() * 0.9;

//内圈矩形
QRectF innerRect;

//计算内圈矩形
if (m_barStyle == StyleLine)
{
innerDiameter = outerDiameter - m_outlinePenWidth;
}

else if(m_barStyle == StyleDonut)
{
innerDiameter = outerDiameter * 0.9;
}

double delta = (outerDiameter - innerDiameter) / 2;

innerRect = QRectF(delta, delta, innerDiameter, innerDiameter);


//画基础图形
drawBase(p, baseRect, innerRect);

//计算当前步长比例
double arcStep = 360.0 / (m_max - m_min) * m_value;

//根据值画出进度条
drawValue(p, baseRect, m_value, arcStep, innerRect, innerDiameter);

//画文字
drawText(p, baseRect, m_value);

p.end();
}

void QRoundProgressBar::drawBase(QPainter &p, const QRectF &baseRect, const QRectF &innerRect)
{
switch (m_barStyle)
{
case StyleDonut:
{
QPainterPath dataPath;
dataPath.setFillRule(Qt::OddEvenFill);
dataPath.moveTo(baseRect.center());
dataPath.addEllipse(innerRect);

QPen pen;
pen.setColor(QColor("#DEE3E7"));
pen.setWidth(10);
p.setPen(pen);
p.setBrush(QBrush(QColor("#FFFFFF")));

p.drawPath(dataPath);
break;
}

case StylePie:
{
p.setPen(QPen(QColor("#FFFFFF"), m_outlinePenWidth));
p.setBrush(QBrush(QColor("#DEE3E7")));
p.drawEllipse(baseRect);
break;
}

case StyleLine:
{
p.setPen(QPen(QColor("#FFFFFF"), m_outlinePenWidth));
p.setBrush(Qt::NoBrush);
p.drawEllipse(baseRect.adjusted(m_outlinePenWidth / 2, m_outlinePenWidth / 2, -m_outlinePenWidth / 2, -m_outlinePenWidth / 2));
break;
}

default:
{
break;
}
}
}

void QRoundProgressBar::drawValue(QPainter &p, const QRectF &baseRect , double value, double arcLength
, const QRectF & innerRect, double innerDiameter)
{
if (value == m_min)
{
return;
}

if (m_barStyle == StyleLine)
{
p.setPen(QColor("#2F8DED"));
p.setBrush(Qt::NoBrush);
p.drawArc(baseRect,m_startAngel * 16, -arcLength * 16);
}
else if (m_barStyle == StyleDonut)
{
QPen pen;
pen.setColor(QColor("#2F8DED"));
pen.setWidth(8);
pen.setCapStyle(Qt::RoundCap);
p.setPen(pen);
p.drawArc(innerRect, m_startAngel*16 , -16*arcLength);
}
else
{
//获取中心点坐标
QPointF centerPoint = baseRect.center();
QPainterPath dataPath;
dataPath.setFillRule(Qt::WindingFill);
dataPath.moveTo(centerPoint);
//逆时针画弧长
dataPath.arcTo(baseRect, m_startAngel, -arcLength);
if (m_barStyle == StylePie)
{
dataPath.lineTo(centerPoint);
p.setPen(QPen(QColor("#2F8DED"), m_dataPenWidth));
}
p.setBrush(QBrush(QColor("#2F8DED")));
p.drawPath(dataPath);
}

}

void QRoundProgressBar::drawText(QPainter &p, const QRectF &rect, double value)
{
QString textToDraw = "%";
double percent = (value - m_min) / (m_max - m_min) * 100.0;
textToDraw = QString::number(percent, 'f', m_decimals) + textToDraw;

QFont f;
f.setFamily("微软雅黑");
f.setPixelSize(20);

p.setFont(f);
p.setPen(QColor("#606266"));

p.drawText(rect, Qt::AlignCenter, textToDraw);
}

【Qt】圆形进度条
https://hodlyounger.github.io/B_Code/Qt/动画/【Qt】圆形进度条/
作者
mingming
发布于
2023年10月27日
许可协议