Qt自定义控件实现简易仪表盘自定义控件实现简易仪表盘
本文实例为大家分享了Qt自定义控件实现简易仪表盘的具体代码,供大家参考,具体内容如下
Qt自定义控件12:简易仪表盘(根据liudianwu大神的界面自己写的代码,建议去学习刘大神,会受益良多的)
先看效果图:
思路:画270度的圆弧,圆弧根据占比分为两种颜色,根据占比在圆弧上画出一个圆球作为标志,然后就是刻度线和刻度值。
刻度线是根据坐标系旋转画出,刻度值是根据角度求出x,y坐标值构造出一个矩形画出刻度值(不要用坐标系旋转画刻度值,
那样刻度值的角度也会旋转,写出的字不是正的,效果不好)。最后就是在中心画value。
关键代码:
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){
评论0