QTmotormeter 程序

#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);

}

相关推荐
huanggang9822 小时前
在Ubuntu22.04上使用Qt Creator开发ROS2项目
qt·ros2
老秦包你会3 小时前
Qt第三课 ----------容器类控件
开发语言·qt
spygg4 小时前
Qt低版本多网卡组播bug
qt·组播·多网卡组播·qt5.7.0
码农客栈4 小时前
qt QWebSocketServer详解
qt
plmm烟酒僧5 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
Black_Friend6 小时前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC6 小时前
【Qt】QTreeView 和 QStandardItemModel的关系
qt
冷凝女子7 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
苏三有春9 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr9 小时前
C++ QT
java·c++·qt