提交 | 用户 | age
|
d3687b
|
1 |
#include "qmyroundindicator.h" |
Q |
2 |
#include <QPen> |
|
3 |
#include <QBrush> |
|
4 |
#include <QPainter> |
|
5 |
#include <QTimer> |
|
6 |
|
|
7 |
QmyRoundIndicator::QmyRoundIndicator(QWidget *parent) : QFrame(parent) |
|
8 |
{ |
|
9 |
QTimer* timer = new QTimer(this); |
|
10 |
timer->start(50); |
|
11 |
connect(timer,&QTimer::timeout,this,&QmyRoundIndicator::OnTimerd); |
|
12 |
} |
|
13 |
|
|
14 |
|
|
15 |
void QmyRoundIndicator::OnTimerd() |
|
16 |
{ |
|
17 |
nCount++; |
|
18 |
update(); |
|
19 |
} |
|
20 |
|
|
21 |
void QmyRoundIndicator::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 |
if (w!=oldw) {displayPosition=0; oldw=w;} |
|
31 |
int s; |
|
32 |
int wmargin=0; |
|
33 |
int hmargin=0; |
|
34 |
if (w>h) { |
|
35 |
s= h; |
|
36 |
wmargin=(w-s)/2; |
|
37 |
|
|
38 |
}else{ |
|
39 |
s= w; |
|
40 |
hmargin=(h-s)/2; |
|
41 |
} |
|
42 |
//画边框 |
|
43 |
QPen pen0(QColor("#4D9CF8")); |
|
44 |
painter.setPen(pen0); |
|
45 |
painter.drawRoundRect(this->rect(),5,5); |
|
46 |
|
|
47 |
int thick = s/16; |
|
48 |
|
|
49 |
QPen pen1(QColor(80,80,80)); |
|
50 |
|
|
51 |
//绘制弧线的背景 |
|
52 |
pen1.setWidth(thick); |
|
53 |
pen1.setCapStyle(Qt::RoundCap); |
|
54 |
// pen.setColor(QColor("#4D9CF8")); |
|
55 |
painter.setPen(pen1); |
|
56 |
|
|
57 |
QRectF in_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); |
|
58 |
int in_startAngle = (0) * 16; |
|
59 |
int in_spanAngle = (180)*16; |
|
60 |
painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); |
|
61 |
|
|
62 |
|
|
63 |
// in_startAngle = (nOffset + 90+270) * 16; |
|
64 |
// in_spanAngle = (45)*16; |
|
65 |
// painter.drawArc(in_rectangle,in_startAngle,in_spanAngle); |
|
66 |
|
|
67 |
int nOffset = nCount*5 % 180; |
|
68 |
|
|
69 |
// position = nOffset *range /180; |
|
70 |
float diff = position - displayPosition; |
|
71 |
if (diff > 5) {displayPosition += 5; } |
|
72 |
else if (diff <=5 && diff >= -5) {displayPosition = position;} |
|
73 |
else if (diff <-5) {displayPosition -=5;} |
|
74 |
|
|
75 |
//画目标刻度 |
|
76 |
target; |
|
77 |
QPen pen22(QColor("#ffffff")); |
|
78 |
pen22.setWidth(thick+2); |
|
79 |
pen22.setCapStyle(Qt::RoundCap); |
|
80 |
|
|
81 |
QRectF tar_rectangle1(wmargin+10, hmargin+s/4, s-20, s-20); |
|
82 |
int tar_startAngle = 180*16-1*((target / range)*180)*16 -1 ; |
|
83 |
int tar_spanAngle = 1; |
|
84 |
painter.setPen(pen22); |
|
85 |
painter.drawArc(tar_rectangle1,tar_startAngle,tar_spanAngle); |
|
86 |
// 目标位置; |
|
87 |
|
|
88 |
//画当前值 |
|
89 |
QPen pen2(QColor("#4D9CF8")); |
|
90 |
pen2.setWidth(thick); |
|
91 |
pen2.setCapStyle(Qt::RoundCap); |
|
92 |
|
|
93 |
QRectF sec_rectangle(wmargin+10, hmargin+s/4, s-20, s-20); |
|
94 |
int sec_startAngle = 180 * 16; |
|
95 |
int sec_spanAngle = -1*((displayPosition / range)*180)*16; |
|
96 |
|
|
97 |
QConicalGradient gradient; |
|
98 |
gradient.setCenter(sec_rectangle.center()); |
|
99 |
gradient.setAngle(190); |
|
100 |
gradient.setColorAt(0, color2); |
|
101 |
gradient.setColorAt(0.5, color2); |
|
102 |
gradient.setColorAt(1, color1); |
|
103 |
|
|
104 |
|
|
105 |
// int arcLengthApproximation = m_width + m_width / 3; |
|
106 |
QPen pen3(QBrush(gradient), thick); |
|
107 |
pen3.setCapStyle(Qt::RoundCap); |
|
108 |
painter.setPen(pen3); |
|
109 |
painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle); |
|
110 |
|
|
111 |
//绘制文本,绘制白色的文字 |
|
112 |
painter.setPen(QPen(QColor(255,255,255))); |
|
113 |
QString s1; |
|
114 |
s1=sTitle; |
|
115 |
//显示标题 |
|
116 |
QRect rect2(wmargin + 20, hmargin +10, s-40,s/8); |
|
117 |
painter.setFont(QFont("Microsoft YaHei",s/12)); |
|
118 |
// painter.drawText(this->rect(),s1); |
|
119 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
120 |
//显示前后刻度 |
|
121 |
painter.setFont(QFont("Microsoft YaHei",s/18)); |
|
122 |
s1="0"; |
|
123 |
// painter.drawText(this->rect(),s1); |
|
124 |
rect2.setRect(wmargin,hmargin+s*3/4+thick/2,20,s/16); |
|
125 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
126 |
|
|
127 |
s1=QString::number(range); |
|
128 |
// painter.drawText(this->rect(),s1); |
|
129 |
rect2.setRect(wmargin+s-50,hmargin+s*3/4+thick/2,80,s/16); |
|
130 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
131 |
|
|
132 |
//显示当前数量 |
|
133 |
painter.setFont(QFont("Microsoft YaHei",s/8)); |
|
134 |
s1=QString::number(displayPosition); |
|
135 |
// painter.drawText(this->rect(),s1); |
|
136 |
rect2.setRect(wmargin+10,hmargin+10,s-20,s-20); |
|
137 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
138 |
//显示单位 |
|
139 |
painter.setFont(QFont("Microsoft YaHei",s/16)); |
|
140 |
s1=sUnit; |
|
141 |
// painter.drawText(this->rect(),s1); |
|
142 |
rect2.setRect(wmargin+10,hmargin+10+s/5,s-20,s-20); |
|
143 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
144 |
|
|
145 |
// 显示目标 |
|
146 |
//画目标线 |
|
147 |
|
|
148 |
//写目标字 |
|
149 |
|
|
150 |
return; |
|
151 |
// QRect rect2(wmargin + 20, hmargin +20, s-40,s-40); |
|
152 |
|
|
153 |
//添加绘制的字体和字号 |
|
154 |
s1=QString::number(int(displayPosition)); |
|
155 |
int l = s1.length(); |
|
156 |
double fr = sqrt(l*l + 4); |
|
157 |
painter.setFont(QFont("Microsoft YaHei",(s-20)/fr)); |
|
158 |
// painter.drawText(this->rect(),s1); |
|
159 |
painter.drawText(rect2,Qt::AlignCenter,s1); |
|
160 |
|
|
161 |
} |
|
162 |
|
|
163 |
void QmyRoundIndicator::SetRange(int n) |
|
164 |
{ |
|
165 |
range=n; |
|
166 |
} |
|
167 |
void QmyRoundIndicator::SetValue(float n) |
|
168 |
{ |
|
169 |
position=n; |
|
170 |
update(); |
|
171 |
} |
|
172 |
void QmyRoundIndicator::SetTarget(float n) |
|
173 |
{ |
|
174 |
target=n; |
|
175 |
update(); |
|
176 |
} |
|
177 |
void QmyRoundIndicator::SetTitle(QString title) |
|
178 |
{ |
|
179 |
sTitle = title; |
|
180 |
} |
|
181 |
void QmyRoundIndicator::SetUnit(QString unit) |
|
182 |
{ |
|
183 |
sUnit = unit; |
|
184 |
} |
|
185 |
void QmyRoundIndicator::SetColor(QColor Start,QColor End) |
|
186 |
{ |
|
187 |
color1 = Start; |
|
188 |
color2 = End; |
|
189 |
} |
|
190 |
|