1
1
#include " Loading.h"
2
+ #include " qmath.h"
3
+ #include < QDebug>
2
4
3
- Loading::Loading (QWidget *parent) : QWidget(parent)
5
+ Loading::Loading (QWidget *parent) : QWidget(parent),_i( 0 ),_interval( 50 ),_index( 0 )
4
6
{
5
- setDotColor (QColor (100 ,100 ,100 ));
7
+ // 设置背景透明
8
+ // this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
9
+ // this->setAttribute(Qt::WA_TranslucentBackground, true);
10
+
11
+ setDotColor (QColor (49 , 177 , 190 ));
12
+ setDotCount (20 );
13
+ connect (&timer,&QTimer::timeout,this ,&Loading::refresh);
14
+ setMaxDiameter (30 );
15
+ setMinDiameter (5 );
6
16
}
7
17
8
18
// ********************************************** 设置部分 *************************************
9
19
// 设置点的个数
10
20
void Loading::setDotCount (int count)
11
21
{
12
-
22
+ _count=count;
13
23
}
14
24
15
25
// 设置点的颜色
@@ -18,16 +28,88 @@ void Loading::setDotColor(const QColor & color)
18
28
_dotColor=color;
19
29
}
20
30
31
+ // 开始动画
32
+ void Loading::start ()
33
+ {
34
+ timer.setInterval (_interval);
35
+ timer.start ();
36
+ }
37
+
38
+ // 设置最大直径
39
+ void Loading::setMaxDiameter (float max)
40
+ {
41
+ _maxDiameter=max;
42
+ }
43
+
44
+ // 设置最小直径
45
+ void Loading::setMinDiameter (float min)
46
+ {
47
+ _minDiameter=min;
48
+ }
21
49
// ********************************************** 绘制部分 *************************************
50
+ // 刷新界面
51
+ void Loading::refresh ()
52
+ {
53
+ repaint ();
54
+ }
55
+
56
+ void Loading::resizeEvent (QResizeEvent *event)
57
+ {
58
+ Q_UNUSED (event)
59
+ caculate ();
60
+ }
61
+
22
62
void Loading::paintEvent (QPaintEvent *event)
23
63
{
64
+ Q_UNUSED (event)
24
65
QPainter painter (this );
25
66
painter.setRenderHint (QPainter::Antialiasing);
26
67
68
+ painter.setPen (_dotColor);
69
+ painter.setBrush (_dotColor);
70
+
71
+ // 绘制点
27
72
paintDot (painter);
28
73
}
29
74
75
+ // 计算绘制正方形区域
76
+ void Loading::caculate ()
77
+ {
78
+ _squareWidth=qMin (this ->width (),this ->height ());
79
+ float half=_squareWidth/2 ;
80
+ _centerDistance=half-_maxDiameter/2 -1 ;
81
+
82
+ float gap=(_maxDiameter-_minDiameter)/(_count-1 )/2 ;
83
+ float angleGap=(float )360 /_count;
84
+
85
+ locationList.clear ();
86
+ radiiList.clear ();
87
+
88
+ for (int i=0 ;i<_count;i++)
89
+ {
90
+ radiiList<<_maxDiameter/2 -i*gap;
91
+ float radian=qDegreesToRadians (-angleGap*i);
92
+ locationList.append (Location (half+_centerDistance*qCos (radian),half-_centerDistance*qSin (radian)));
93
+ }
94
+ }
95
+
96
+ // 绘制圆点
30
97
void Loading::paintDot (QPainter& painter)
31
98
{
99
+ for (int i=0 ;i<_count;i++)
100
+ {
101
+ painter.setPen (_dotColor);
102
+ // 半径
103
+ float radii=radiiList.at ((_index+_count-i)%_count);
32
104
105
+ // 绘制圆点
106
+ painter.drawEllipse (QPointF (locationList.at (i).x ,locationList.at (i).y ),radii,radii);
107
+ // 绘制正方形
108
+ // painter.drawRect(locationList.at(i).x,locationList.at(i).y,radii,radii);
109
+ // 绘制文字
110
+ // QFont font("Microsoft YaHei",radii*1.2,75);
111
+ // painter.setFont(font);
112
+ // painter.drawText(QPointF(locationList.at(i).x,locationList.at(i).y),u8"霞");
113
+ }
114
+ _index++;
33
115
}
0 commit comments