QuakeGod
2023-05-30 d3687ba77720285bac5e7ff51e7e65437e71c00f
提交 | 用户 | age
d3687b 1 #include "qmyroundprogress.h"
Q 2 #include <QPen>
3 #include <QBrush>
4 #include <QPainter>
5 #include <QTimer>
6
7 QMyRoundProgress::QMyRoundProgress(QWidget *parent) : QWidget(parent)
8 {
9     QTimer* timer = new QTimer(this);
10         timer->start(50);
11         connect(timer,&QTimer::timeout,this,&QMyRoundProgress::OnTimerd);
12 }
13
14
15 void QMyRoundProgress::OnTimerd()
16 {
17     nCount++;
18     update();
19 }
20
21 void QMyRoundProgress::paintEvent(QPaintEvent *event)
22 {
23     //控件的绘制工具,通过该工具我们可以进行常用的绘制了
24     QPainter painter(this);
25      painter.setRenderHint(QPainter::Antialiasing);
26
27         //取得画布大小和中心方形。
28      int w = this->rect().width();
29      int h = this->rect().height();
30      int s;
31      int wmargin=0;
32      int hmargin=0;
33      if (w>h) {
34          s= h;
35          wmargin=(w-s)/2;
36
37      }else{
38          s= w;
39          hmargin=(h-s)/2;
40      }
41      //画边框
42      QPen pen0(QColor("#4D9CF8"));
43      painter.setPen(pen0);
44      painter.drawRoundRect(this->rect(),5,5);
45
46       int thick =  s/20;
47
48      QPen pen1(QColor(80,80,80));
49
50      //绘制弧线的背景
51       pen1.setWidth(thick);
52       pen1.setCapStyle(Qt::RoundCap);
53       //      pen.setColor(QColor("#4D9CF8"));
54       painter.setPen(pen1);
55
56      QRectF in_rectangle(wmargin+10, hmargin+s/4, s-20, s-20);
57      int in_startAngle = (0) * 16;
58      int in_spanAngle = (180)*16;
59      painter.drawArc(in_rectangle,in_startAngle,in_spanAngle);
60
61
62 //     in_startAngle = (nOffset + 90+270) * 16;
63 //     in_spanAngle = (45)*16;
64 //     painter.drawArc(in_rectangle,in_startAngle,in_spanAngle);
65
66      int nOffset = nCount*5 % 180;
67
68      position = nOffset *range /180;
69
70      QPen pen2(QColor("#4D9CF8"));
71      pen2.setWidth(thick);
72      pen2.setCapStyle(Qt::RoundCap);
73
74      QRectF sec_rectangle(wmargin+10, hmargin+s/4, s-20, s-20);
75      int sec_startAngle = 180 * 16;
76      int sec_spanAngle = -1*((position / range)*180)*16;
77
78      QConicalGradient gradient;
79      gradient.setCenter(sec_rectangle.center());
80      gradient.setAngle(190);
81      gradient.setColorAt(0, color2);
82      gradient.setColorAt(0.5, color2);
83      gradient.setColorAt(1, color1);
84
85
86  //    int arcLengthApproximation = m_width + m_width / 3;
87      QPen pen3(QBrush(gradient), thick);
88      pen3.setCapStyle(Qt::RoundCap);
89      painter.setPen(pen3);
90      painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle);
91
92
93
94      //绘制文本,绘制白色的文字
95      painter.setPen(QPen(QColor(255,255,255)));
96      QString s1;
97      s1=sTitle;
98      //显示标题
99      QRect rect2(wmargin + 20, hmargin +10, s-40,s/8);
100      painter.setFont(QFont("Microsoft YaHei",s/12));
101   //   painter.drawText(this->rect(),s1);
102     painter.drawText(rect2,Qt::AlignCenter,s1);
103      //显示前后刻度
104     painter.setFont(QFont("Microsoft YaHei",s/18));
105     s1="0";
106  //   painter.drawText(this->rect(),s1);
107     rect2.setRect(wmargin,hmargin+s*3/4,20,s/16);
108    painter.drawText(rect2,Qt::AlignCenter,s1);
109
110    s1=QString::number(range);
111 //   painter.drawText(this->rect(),s1);
112    rect2.setRect(wmargin+s-30,hmargin+s*3/4,40,s/16);
113   painter.drawText(rect2,Qt::AlignCenter,s1);
114
115      //显示当前数量
116   painter.setFont(QFont("Microsoft YaHei",s/8));
117   s1=QString::number(position);
118 //   painter.drawText(this->rect(),s1);
119   rect2.setRect(wmargin+10,hmargin+10-s/20,s-20,s-20);
120  painter.drawText(rect2,Qt::AlignCenter,s1);
121      //显示单位
122  painter.setFont(QFont("Microsoft YaHei",s/16));
123  s1=sUnit;
124 //   painter.drawText(this->rect(),s1);
125  rect2.setRect(wmargin+10,hmargin+10+s/10,s-20,s-20);
126 painter.drawText(rect2,Qt::AlignCenter,s1);
127
128     return;
129     //   QRect rect2(wmargin + 20, hmargin +20, s-40,s-40);
130
131      //添加绘制的字体和字号
132      s1=QString::number(int(position));
133      int l = s1.length();
134      double fr = sqrt(l*l + 4);
135      painter.setFont(QFont("Microsoft YaHei",(s-20)/fr));
136   //   painter.drawText(this->rect(),s1);
137     painter.drawText(rect2,Qt::AlignCenter,s1);
138
139 }
140
141 void QMyRoundProgress::SetRange(int n)
142 {
143     range=n;
144 }
145 void QMyRoundProgress::SetPosition(float n)
146 {
147     position=n;
148     update();
149 }
150 void QMyRoundProgress::SetTitle(QString title)
151 {
152     sTitle = title;
153 }
154 void QMyRoundProgress::SetUnit(QString unit)
155 {
156     sUnit = unit;
157 }
158 void QMyRoundProgress::SetColor(QColor Start,QColor End)
159 {
160     color1 = Start;
161     color2 = End;
162 }
163
164