#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
#include <QtMath>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
qreal qValues;
const static int radius;
const static int maxvelocity;
const static int minvelocity;
QTimer *timer;
bool _isForward;
public:
void setvalue(qreal iValues);
void paintEvent(QPaintEvent *event);
private slots:
void inputValue();
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
const int Widget::radius=150;
const int Widget::maxvelocity=400;
const int Widget::minvelocity=0;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
qValues=0;
_isForward=true;
timer=new QTimer(this);
timer->setInterval(100);
timer->start();
connect(timer,&QTimer::timeout,this,&Widget::inputValue);
}
Widget::~Widget()
{
delete ui;
}
void Widget::setvalue(qreal iValues)
{
qValues = iValues;
update();
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPoint center(width()/2,height()/2);
painter.save();
painter.translate(center);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(142,142,142));
const int backgroundradius=radius+3;
painter.drawEllipse(QPoint(),backgroundradius,backgroundradius);
painter.rotate(-135);
const int longstep=10;
const int shortstep=50;
for(int i=0;i<=shortstep;i++)
{
painter.setPen((i<shortstep*0.8)?QColor(32,243,32):QColor(243,32,32));
if (i%(shortstep/longstep)!=0)
{
QPoint p1(0,-(radius-8));
QPoint p2(0,-radius);
painter.drawLine(p1,p2);
}
else
{
QPoint p1(0,-(radius-14));
QPoint p2(0,-radius);
painter.drawLine(p1,p2);
}
painter.rotate(270.0/shortstep);
}
painter.restore();
QFontMetrics fm=painter.fontMetrics();
int tx=center.x();
int ty=int(center.y()+(radius/1.4142));
QString title="yibiaopan";
QSize tsz=fm.size(0,title);
QRect trect(QPoint(tx-tsz.width()/2,ty-tsz.height()/2),tsz);
painter.setPen(QColor(127,127,127));
painter.setBrush(QColor(173,163,163));
painter.drawRoundRect(trect.adjusted(-6,-4,6,4),3,3);
painter.setPen(QColor(243,243,243));
painter.setBrush(Qt::NoBrush);
painter.drawText(trect,title);
painter.setPen(QColor(243,243,243));
const int hand=radius-21;
for(int i=0;i<=longstep;i++)
{
qreal angle=qDegreesToRadians(-135.0+i*(270.0/longstep));
QString etext=QString::number(minvelocity+i*(maxvelocity-minvelocity)/longstep);
QPointF dirvec(qSin(angle),-qCos(angle));
QPointF ecenter(center.x()+hand*dirvec.x(),center.y()+hand*dirvec.y());
QSize esz=fm.size(0,etext);
QRectF erect(QPointF(ecenter.x()-esz.width()/2,ecenter.y()-esz.height()/2),esz);
painter.drawText(erect,etext);
}
painter.save();
QPoint triangle[]={
{-5,0},
{0,25-radius},
{5,0}
};
qreal degree=-135.0+270.0*(qValues-minvelocity)/(maxvelocity-minvelocity);
painter.translate(center);
painter.rotate(degree);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::red);
painter.drawPolygon(triangle,3);
painter.setPen(QPen(QColor(214,0,0),2));
painter.drawEllipse(QPoint(),7,7);
painter.restore();
}
void Widget::inputValue()
{
if(_isForward)
qValues+=1;
else
qValues-=1;
if(qValues>=maxvelocity)
{
qValues=maxvelocity;
_isForward=false;
}
if(qValues<=minvelocity)
{
qValues=minvelocity;
_isForward=true;
}
setvalue(qValues);
}