效果如下:
头文件:
#ifndef DASHBOARD_H
#define DASHBOARD_H
#include <QWidget>
#include <QPaintEvent>
#include <QTimerEvent>
class Dashboard : public QWidget
{
Q_OBJECT
public:
explicit Dashboard(QWidget *parent = nullptr);
~Dashboard();
protected:
void drawNumber(QPainter &painter,QPoint start,int n);
void drawPoint(QPainter &painter,QPoint center,qreal angle);
void paintEvent(QPaintEvent *event);
void timerEvent(QTimerEvent *event);
private:
int m_nPointAngle = 0;
int m_nTimerID = -1;
};
#endif // DASHBOARD_H
源文件
#include "dashboard.h"
#include <QRadialGradient>
#include <QPainter>
#include <QtMath>
#include <QFont>
QPoint rotatePoint(QPoint center, QPoint p1, qreal angle)
{
qreal x = center.x() + (p1.x() - center.x()) * qCos(qDegreesToRadians(angle)) -
(p1.y() - center.y()) * qSin(qDegreesToRadians(angle));
qreal y = center.y() + (p1.y() - center.y()) * qCos(qDegreesToRadians(angle)) +
(p1.x() - center.x()) * qSin(qDegreesToRadians(angle));
return QPoint(x,y);
}
Dashboard::Dashboard(QWidget *parent) : QWidget(parent)
{
this->resize(500,500);
m_nTimerID = this->startTimer(1000);
}
Dashboard::~Dashboard()
{
if(m_nTimerID != -1){
this->killTimer(m_nTimerID);
}
}
void Dashboard::drawNumber(QPainter &painter, QPoint start, int n)
{
QFont font("YaHei", 20, QFont::Bold);
painter.setFont(font);
QString text = QString::number(n);
int w = painter.fontMetrics().width(text);
int h = painter.fontMetrics().height();
switch (n) {
case 0:
start += QPoint(-w/2,-3);
break;
case 1:
start += QPoint(0,-5);
break;
case 2:
start += QPoint(2,0);
break;
case 3:
start += QPoint(2,h/2 - 3);
break;
case 4:
start += QPoint(2,h/2 - 3);
break;
case 5:
start += QPoint(0,h - 3);
break;
case 6:
start += QPoint(-w / 2,h);
break;
case 7:
start += QPoint(-w / 2 - 2,h);
break;
case 8:
start += QPoint(-w - 3,h / 2);
break;
case 9:
start += QPoint(- w -3,h/2 - 3);
break;
case 10:
start += QPoint(-w,0);
break;
default:
break;
}
painter.drawText(start,QString::number(n));
}
void Dashboard::drawPoint(QPainter &painter, QPoint center, qreal angle)
{
painter.setPen(QPen(QColor(156,240,249)));
QPoint end = center + QPoint(180,0);
end = rotatePoint(center,end,angle + 90);
QPoint p1 = center + QPoint(-5,0);
QPoint p2 = center + QPoint(5,0);
QPainterPath path;
path.moveTo(p1);
path.lineTo(p2);
path.lineTo(end);
path.lineTo(p1);
painter.drawPath(path);
painter.fillPath(path,QBrush(QColor(156,240,249)));
}
void Dashboard::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.fillRect(QRect(QPoint(0,0),this->size()),QColor(64,64,64));
painter.setRenderHint(QPainter::Antialiasing, true);
QPoint center(250,250);
int r1 = 200;
int r2 = 220;
//绘制外圈
QRadialGradient radialColor;
radialColor.setCenter(center);
radialColor.setRadius(r2);
radialColor.setFocalPoint(center);
radialColor.setFocalRadius(r2 - 20);
radialColor.setColorAt(0,Qt::black);
radialColor.setColorAt(1,QColor(248,31,14));
painter.save();
painter.setBrush(QBrush(radialColor));
painter.drawPie(QRectF(center.x() - r2,center.y() - r2,r2 * 2 ,r2 * 2),-30 * 16,120 * 16);
painter.restore();
//绘制内圈
QRadialGradient radialColor2;
radialColor2.setCenter(center);
radialColor2.setRadius(r1);
radialColor2.setFocalPoint(center);
radialColor2.setFocalRadius(r1 - 20);
radialColor2.setColorAt(0,Qt::black);
radialColor2.setColorAt(1,QColor(91,158,238));
painter.save();
painter.setBrush(QBrush(radialColor2));
painter.drawEllipse(center.x() - r1,center.y() - r1,r1 * 2,r1 * 2);
painter.restore();
//绘制指针
painter.save();
drawPoint(painter,center,m_nPointAngle);
painter.restore();
//绘制中心
painter.save();
painter.setBrush(QColor(150,200,210));
painter.drawEllipse(center.x() - 5,center.y() - 5,10,10);
painter.restore();
//绘制刻度
painter.save();
for (int i = 90; i <= 390; i += 1) {
//内圈数据
QPoint p0_s = center + QPoint(r1 - 5,0);
QPoint p0_e = center + QPoint(r1,0);
QPoint s = rotatePoint(center,p0_s,i);
QPoint e = rotatePoint(center,p0_e,i);
painter.setPen(QPen(Qt::white,2));
if(i % 30 == 0){
painter.drawLine(s,e); //绘制内圈刻度
//绘制内圈数字
drawNumber(painter,s,(i - 90) / 30);
}
//外圈数据
if(i % 15 == 0){
p0_s = center + QPoint(r1,0);
p0_e = center + QPoint(r1 + 5,0);
}else if(i % 3 == 0){
p0_s = center + QPoint(r1 + 1,0);
p0_e = center + QPoint(r1 + 3,0);
painter.setPen(QPen(Qt::white,1));
}else{
continue;
}
s = rotatePoint(center,p0_s,i);
e = rotatePoint(center,p0_e,i);
painter.drawLine(s,e);
}
painter.restore();
}
void Dashboard::timerEvent(QTimerEvent *event)
{
QWidget::timerEvent(event);
m_nPointAngle += 1;
if(m_nPointAngle > 300){
m_nPointAngle = 0;
}
update();
}