服务器之家

服务器之家 > 正文

Qt自定义控件实现简易仪表盘

时间:2021-08-10 15:39     来源/作者:parkchorong

本文实例为大家分享了Qt自定义控件实现简易仪表盘的具体代码,供大家参考,具体内容如下

Qt自定义控件12:简易仪表盘(根据liudianwu大神的界面自己写的代码,建议去学习刘大神,会受益良多的)

先看效果图:

Qt自定义控件实现简易仪表盘

思路:画270度的圆弧,圆弧根据占比分为两种颜色,根据占比在圆弧上画出一个圆球作为标志,然后就是刻度线和刻度值。刻度线是根据坐标系旋转画出,刻度值是根据角度求出x,y坐标值构造出一个矩形画出刻度值(不要用坐标系旋转画刻度值,那样刻度值的角度也会旋转,写出的字不是正的,效果不好)。最后就是在中心画value。

关键代码:

?
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
void CMPassrate5::paintEvent(QPaintEvent *event){
  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);
 
  drawE(&painter);
  drawEPoint(&painter);
  drawLine(&painter);
  drawEText(&painter);
  drawValue(&painter);
}
 
void CMPassrate5::drawE(QPainter* painter){
  QRect rect(-radius,-radius,2*radius,2*radius);
  painter->save();
  painter->setPen(Qt::NoPen);
 
  QPainterPath path;
  QPainterPath subPath;
  QPainterPath outPath;
  QPainterPath outPubPath;
  outPath.arcTo(rect,-45,outRange);
  outPubPath.addEllipse(rect.adjusted(side,side,-side,-side));
  outPath -= outPubPath;
  color.setAlpha(100);
  painter->setBrush(color);
  painter->drawPath(outPath);
 
 
  path.arcTo(rect,-45+outRange,range);
  subPath.addEllipse(rect.adjusted(4,4,-4,-4));
  path -= subPath;
  color.setAlpha(180);
  painter->setBrush(color);
  painter->drawPath(path);
 
  painter->restore();
}
 
void CMPassrate5::drawEPoint(QPainter* painter){
  //圆球位置就在outRange尽头处
  painter->save();
  color.setAlpha(180);
  painter->setPen(Qt::NoPen);
  painter->setBrush(color);
 
  float x = (radius-side/2)*qCos((range+135)*3.14/180);
  float y = (radius-side/2)*qSin((range+135)*3.14/180);
  qDebug()<<"x:"<<x<<" y:"<<y;
  painter->drawEllipse(QPoint(x,y),side,side);
  painter->restore();
}
 
void CMPassrate5::drawLine(QPainter* painter){
  painter->save();
  painter->rotate(135);
  color.setAlpha(100);
  painter->setPen(color);
  QLine line(QPoint(radius-side-lineLength,0),QPoint(radius-side,0));
 
  for(int i = 0;i<lineCount;i++){
    painter->drawLine(line);
    painter->rotate(270.0/lineCount);
  }
  painter->restore();
}
 
void CMPassrate5::drawEText(QPainter* painter){
  painter->save();
//  painter->rotate(135);
  painter->setPen(Qt::black);
  float textRange = 270.0/(textCount-1);
  float x,y;
  for(int i = 0;i<=10;i++){
    x = (radius-side-lineLength)*qCos((textRange*i+135)*3.14/180);
    y = (radius-side-lineLength)*qSin((textRange*i+135)*3.14/180);
    if(i<5){
      QRect rect(x,y-4,20,10);
      painter->drawText(rect,Qt::AlignCenter,QString::number(i*10));
    }else if(i ==5){
      QRect rect(x-7,y,20,10);
      painter->drawText(rect,Qt::AlignCenter,QString::number(i*10));
    }else{
      QRect rect(x-20,y-5,20,10);
      painter->drawText(rect,Qt::AlignCenter,QString::number(i*10));
    }
 
  }
  painter->restore();
}
 
void CMPassrate5::drawValue(QPainter* painter){
  painter->save();
  QPen pen = painter->pen();
  pen.setColor(color);
  pen.setWidth(2);
  painter->setPen(pen);
  QFont font = painter->font();
  font.setPixelSize(45);
  painter->setFont(font);
 
  QRect rect(-25,-25,50,50);
  painter->drawText(rect,Qt::AlignCenter,QString::number(value));
  painter->restore();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/parkchorong/article/details/102552660

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部