HMI-20-[刷新问题]解决界面刷新卡顿问题
【摘要】
基于Qt的汽车仪表模拟
先描述现象,就是我的这两个表转速表和速度表,他们按照正常的是同时开始转,完了再同时回到起点,但是呢,我在其他的电脑上实验的时候,发现会有不同步显现,特别明显。
所以,我担心是我的频繁刷新导致的,因为我的刷新频率太高了
每个表里面有两个定时器,定时10毫秒,每个定时器对应的操作里面都有upd...
基于Qt的汽车仪表模拟
先描述现象,就是我的这两个表转速表和速度表,他们按照正常的是同时开始转,完了再同时回到起点,但是呢,我在其他的电脑上实验的时候,发现会有不同步显现,特别明显。
所以,我担心是我的频繁刷新导致的,因为我的刷新频率太高了
每个表里面有两个定时器,定时10毫秒,每个定时器对应的操作里面都有updata这样等于我一个表每秒就要刷新100*100 次,两个表,同时刷新,那就是100*100*100*100 ,着个刷新频率可能真的是太高了,
剩下的呢,就是把原来的仪表的自检函数优化,增加线程,完美解决问题。但是目前的版本还是没有优化的版本,
看下修改后的代码吧,修改之前的改吗可以看前面的内容
-
#ifndef TACHOMETER_H
-
#define TACHOMETER_H
-
-
#include <QWidget>
-
#include <QPainter>
-
#include <QPaintEvent>
-
#include <QThread>
-
-
class TachoThread : public QThread
-
{
-
Q_OBJECT
-
public:
-
/**
-
* @brief run
-
* 重载线程函数
-
*/
-
virtual void run();
-
-
signals:
-
void signal_selfInspection(int number);
-
void signal_finished();
-
private:
-
int m_number = 0;
-
bool flagDESC = false;
-
};
-
-
namespace Ui {
-
class Tachometer;
-
}
-
-
class Tachometer : public QWidget
-
{
-
Q_OBJECT
-
-
public:
-
explicit Tachometer(QWidget *parent = 0);
-
~Tachometer();
-
/**
-
* @brief selfInspection
-
* 转速表自检程序
-
*/
-
void selfInspection();
-
/**
-
* @brief setTacho
-
* @param tacho
-
* 设置发动机转速0-8000 转速
-
*/
-
void setTacho(int tacho = 0);
-
/**
-
* @brief setTemperature
-
* @param temperatur
-
* 设置温度
-
*/
-
void setTemperature(int temperatur = 0);
-
signals:
-
/**
-
* @brief signalSelfInspection
-
* 自检完成信号
-
*/
-
void signalSelfInspectionfinished();
-
private:
-
/**
-
* @brief eventFilter
-
* @param watched
-
* @param event
-
* @return
-
* 事件过滤器器
-
*/
-
bool eventFilter(QObject *watched, QEvent *event);
-
/**
-
* @brief drawPonter
-
* @param angle
-
* 绘制指针
-
*/
-
void drawPonter(int angle = -135);
-
/**
-
* @brief drawPonterShade
-
* @param angle
-
* 绘制指针阴影
-
*/
-
void drawPonterShade(int angle = -135);
-
/**
-
* @brief drawTemperature
-
* @param temperature
-
* 绘制温度表
-
*/
-
void drawTemperature(int temperature = 0);
-
private slots:
-
/**
-
* @brief slot_tachoSelfInspection
-
* @param number
-
* 完成自检动画
-
*/
-
void slot_tachoSelfInspection(int number);
-
public:
-
int currentTacho = 0; //当前转速
-
int currentTemperature = 0; //当前温度
-
private:
-
Ui::Tachometer *ui;
-
int m_Angle = 0; //转速表角度
-
int m_Temperature = 0;
-
double m_shadeOpacity = 0.0; //指针阴影透明度
-
TachoThread *m_tachoThread = NULL;
-
-
};
-
-
#endif // TACHOMETER_H
-
#include "tachometer.h"
-
#include "ui_tachometer.h"
-
#include <QDebug>
-
Tachometer::Tachometer(QWidget *parent) :QWidget(parent),ui(new Ui::Tachometer)
-
{
-
ui->setupUi(this);
-
ui->label_mainback->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/mainback.png);}");
-
ui->label_flag_temperature->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/temperature_r.png);}");
-
ui->label_pointer->installEventFilter(this);
-
ui->label_pointer_shade->installEventFilter(this);
-
ui->label_pointer_temperature->installEventFilter(this);
-
m_tachoThread = new TachoThread();
-
connect(m_tachoThread,&TachoThread::signal_finished,this,[=](){m_tachoThread->terminate();});
-
connect(m_tachoThread,&TachoThread::signal_selfInspection,this,&Tachometer::slot_tachoSelfInspection);
-
setTacho();
-
}
-
-
Tachometer::~Tachometer()
-
{
-
delete ui;
-
}
-
-
void Tachometer::selfInspection()
-
{
-
if(m_tachoThread)
-
{
-
m_tachoThread->start();
-
}
-
}
-
-
void Tachometer::setTacho(int tacho)
-
{
-
currentTacho = tacho;
-
m_Angle = currentTacho*0.03375-135;
-
update();
-
}
-
-
void Tachometer::setTemperature(int temperatur)
-
{
-
currentTemperature = temperatur;
-
m_Temperature = temperatur;
-
if(m_Temperature >75)
-
{
-
ui->label_flag_temperature->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/temperature_r.png);}");
-
}
-
else
-
{
-
ui->label_flag_temperature->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/temperature_w.png);}");
-
}
-
update();
-
}
-
-
bool Tachometer::eventFilter(QObject *watched, QEvent *event)
-
{
-
if(watched == ui->label_pointer && event->type() == QEvent::Paint)
-
{
-
drawPonter(m_Angle);
-
}
-
if(watched == ui->label_pointer_shade && event->type() == QEvent::Paint)
-
{
-
drawPonterShade(m_Angle);
-
}
-
if(watched == ui->label_pointer_temperature && event->type() == QEvent::Paint)
-
{
-
drawTemperature(m_Temperature);
-
}
-
return QWidget::eventFilter(watched,event);
-
}
-
-
void Tachometer::drawPonter(int angle)
-
{
-
m_shadeOpacity = currentTacho / 10000.0;
-
QPainter painter(ui->label_pointer);
-
painter.setRenderHint((QPainter::SmoothPixmapTransform));
-
painter.setRenderHint(QPainter::HighQualityAntialiasing);
-
painter.translate(304,303.5);
-
painter.save();
-
painter.rotate(angle);
-
painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Tachometer/pointer.png"));
-
painter.restore();
-
}
-
-
void Tachometer::drawPonterShade(int angle)
-
{
-
QPainter painter(ui->label_pointer_shade);
-
painter.setOpacity(m_shadeOpacity);
-
painter.setRenderHint((QPainter::SmoothPixmapTransform));
-
painter.setRenderHint(QPainter::HighQualityAntialiasing);
-
painter.translate(304,303.5);
-
painter.save();
-
painter.rotate(angle);
-
painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Tachometer/pointer_shade.png"));
-
painter.restore();
-
}
-
-
-
void Tachometer::drawTemperature(int temperature)
-
{
-
QPainter painter(ui->label_pointer_temperature);
-
painter.setRenderHint((QPainter::SmoothPixmapTransform));
-
painter.setRenderHint(QPainter::HighQualityAntialiasing);
-
painter.translate(304,303.5);
-
painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
-
painter.save();
-
if(temperature<=25)
-
{
-
painter.drawArc(-304,-308,608,608,247*16,temperature*7);
-
}
-
else if(temperature>25 && temperature <= 50)
-
{
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,(temperature-25)*7);
-
}
-
else if(temperature>50 && temperature <= 75)
-
{
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,24*7);
-
painter.drawArc(-304,-308,608,608,271*16,(temperature-50)*7);
-
}
-
else if(temperature>75 && temperature <= 90)
-
{
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,24*7);
-
painter.drawArc(-304,-308,608,608,271*16,24*7);
-
painter.drawArc(-304,-308,608,608,283*16,(temperature-75)*7);
-
}
-
else if(temperature>90 && temperature <= 100)
-
{
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,24*7);
-
painter.drawArc(-304,-308,608,608,271*16,24*7);
-
painter.drawArc(-304,-308,608,608,283*16,15*7);
-
painter.setPen(QPen(Qt::red,6,Qt::SolidLine));
-
painter.drawArc(-304,-308,608,608,290*16,(temperature-90)*7);
-
}
-
painter.restore();
-
}
-
-
void Tachometer::slot_tachoSelfInspection(int number)
-
{
-
currentTacho = number*80;
-
m_Angle = number*80*0.03375-135;
-
setTemperature(number);
-
}
-
-
void TachoThread::run()
-
{
-
while (true)
-
{
-
emit signal_selfInspection(m_number);
-
if(flagDESC)
-
{
-
-
if(m_number == 0)
-
{
-
emit signal_finished();
-
flagDESC = !flagDESC;
-
}
-
m_number--;
-
}
-
else
-
{
-
m_number++;
-
if(m_number == 100)
-
{
-
flagDESC = !flagDESC;
-
}
-
}
-
msleep(10);
-
}
-
}
-
#ifndef SPEEDMETER_H
-
#define SPEEDMETER_H
-
-
#include <QWidget>
-
#include <QPainter>
-
#include <QPaintEvent>
-
#include <QThread>
-
class SpeedThread : public QThread
-
{
-
Q_OBJECT
-
public:
-
/**
-
* @brief run
-
* 重载线程函数
-
*/
-
virtual void run();
-
-
signals:
-
void signal_selfInspection(int number);
-
void signal_finished();
-
private:
-
int m_number = 0;
-
bool flagDESC = false;
-
};
-
-
namespace Ui {
-
class Speedmeter;
-
}
-
-
class Speedmeter : public QWidget
-
{
-
Q_OBJECT
-
-
public:
-
explicit Speedmeter(QWidget *parent = 0);
-
~Speedmeter();
-
/**
-
* @brief selfInspection
-
* 转速表自检程序
-
*/
-
void selfInspection();
-
/**
-
* @brief setCarSpeed
-
* @param speed
-
* 设置车速
-
*/
-
void setCarSpeed(int speed = 0);
-
/**
-
* @brief setOilMass
-
* @param mass
-
* 设置油量
-
*/
-
void setOilMass(int mass = 0);
-
signals:
-
/**
-
* @brief speedChange
-
* @param speed
-
* 速度改变信号
-
*/
-
void speedChange(int speed);
-
/**
-
* @brief signalSelfInspection
-
* 自检完成信号
-
*/
-
void signalSelfInspectionfinished();
-
private:
-
/**
-
* @brief eventFilter
-
* @param watched
-
* @param event
-
* @return
-
* 事件过滤器器
-
*/
-
bool eventFilter(QObject *watched, QEvent *event);
-
/**
-
* @brief drawPonter
-
* @param angle
-
* 绘制指针
-
*/
-
void drawPonter(int angle = -135);
-
/**
-
* @brief drawPonterShade
-
* @param angle
-
* 绘制指针阴影
-
*/
-
void drawPonterShade(int angle = -135);
-
/**
-
* @brief drawOilMass
-
* @param mess
-
* 绘制油量表
-
*/
-
void drawOilMass(int mass = 0);
-
private slots:
-
/**
-
* @brief slot_tachoSelfInspection
-
* @param number
-
* 完成自检动画
-
*/
-
void slot_carSpeedSelfInspection(int number);
-
public:
-
int currentCarSpeed = 0; //当前车速
-
int currentMass = 0; //当前油量
-
private:
-
Ui::Speedmeter *ui;
-
int m_Angle = 0; //速度角度
-
double m_shadeOpacity = 0.0; //指针阴影透明度
-
int m_OilMass = 0;
-
SpeedThread *m_speedThread = NULL;
-
-
};
-
-
#endif // SPEEDMETER_H
-
#include "speedmeter.h"
-
#include "ui_speedmeter.h"
-
#include <QDebug>
-
Speedmeter::Speedmeter(QWidget *parent) : QWidget(parent),ui(new Ui::Speedmeter)
-
{
-
ui->setupUi(this);
-
ui->label_mainback->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/mainback.png);}");
-
ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_o.png);}");
-
ui->label_pointer->installEventFilter(this);
-
ui->label_pointer_shade->installEventFilter(this);
-
ui->label_oilmass->installEventFilter(this);
-
m_speedThread = new SpeedThread();
-
connect(m_speedThread,&SpeedThread::signal_finished,this,[=](){m_speedThread->terminate();});
-
connect(m_speedThread,&SpeedThread::signal_selfInspection,this,&Speedmeter::slot_carSpeedSelfInspection);
-
setCarSpeed();
-
-
}
-
-
Speedmeter::~Speedmeter()
-
{
-
-
delete ui;
-
}
-
-
void Speedmeter::selfInspection()
-
{
-
if(m_speedThread)
-
{
-
m_speedThread->start();
-
}
-
}
-
-
void Speedmeter::setCarSpeed(int speed)
-
{
-
currentCarSpeed = speed;
-
emit speedChange(currentCarSpeed);
-
m_Angle = currentCarSpeed*1.125-135;
-
update();
-
}
-
-
void Speedmeter::setOilMass(int mass)
-
{
-
if(mass<=10)
-
{
-
ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_o.png);}");
-
}
-
else
-
{
-
ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_w.png);}");
-
}
-
m_OilMass = mass;
-
update();
-
}
-
-
bool Speedmeter::eventFilter(QObject *watched, QEvent *event)
-
{
-
if(watched == ui->label_pointer && event->type() == QEvent::Paint)
-
{
-
drawPonter(m_Angle);
-
}
-
if(watched == ui->label_pointer_shade && event->type() == QEvent::Paint)
-
{
-
drawPonterShade(m_Angle);
-
}
-
if(watched == ui->label_oilmass && event->type() == QEvent::Paint)
-
{
-
drawOilMass(m_OilMass);
-
}
-
return QWidget::eventFilter(watched,event);
-
}
-
-
void Speedmeter::drawPonter(int angle)
-
{
-
m_shadeOpacity = currentCarSpeed / 300.0;
-
QPainter painter(ui->label_pointer);
-
painter.setRenderHint((QPainter::SmoothPixmapTransform));
-
painter.setRenderHint(QPainter::HighQualityAntialiasing);
-
painter.translate(305,303.5);
-
painter.save();
-
painter.rotate(angle);
-
painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/pointer.png"));
-
painter.restore();
-
}
-
-
void Speedmeter::drawPonterShade(int angle)
-
{
-
QPainter painter(ui->label_pointer_shade);
-
painter.setOpacity(m_shadeOpacity);
-
painter.setRenderHint((QPainter::SmoothPixmapTransform));
-
painter.setRenderHint(QPainter::HighQualityAntialiasing);
-
painter.translate(305,303.5);
-
painter.save();
-
painter.rotate(angle);
-
painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/pointer_shade.png"));
-
painter.restore();
-
}
-
-
void Speedmeter::drawOilMass(int mass)
-
{
-
QPainter painter(ui->label_oilmass);
-
painter.setRenderHint((QPainter::SmoothPixmapTransform));
-
painter.setRenderHint(QPainter::HighQualityAntialiasing);
-
painter.translate(305,303.5);
-
painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
-
painter.save();
-
if(mass<=10)
-
{
-
painter.setPen(QPen(Qt::red,6,Qt::SolidLine));
-
painter.drawArc(-304,-308,608,608,247*16,mass*7);
-
}
-
else if(mass>10 && mass<=25)
-
{
-
painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
-
painter.drawArc(-304,-308,608,608,247*16,mass*7);
-
}
-
else if(mass>25 && mass <= 50)
-
{
-
painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,(mass-25)*7);
-
}
-
else if(mass>50 && mass <= 75)
-
{
-
painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,24*7);
-
painter.drawArc(-304,-308,608,608,271*16,(mass-50)*7);
-
}
-
else if(mass>75 && mass <= 100)
-
{
-
painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
-
painter.drawArc(-304,-308,608,608,247*16,24*7);
-
painter.drawArc(-304,-308,608,608,259*16,24*7);
-
painter.drawArc(-304,-308,608,608,271*16,24*7);
-
painter.drawArc(-304,-308,608,608,283*16,(mass-75)*7);
-
}
-
painter.restore();
-
}
-
-
void Speedmeter::slot_carSpeedSelfInspection(int number)
-
{
-
m_OilMass = number;
-
setCarSpeed(number*2.4);
-
}
-
void SpeedThread::run()
-
{
-
while (true)
-
{
-
emit signal_selfInspection(m_number);
-
if(flagDESC)
-
{
-
-
if(m_number == 0)
-
{
-
emit signal_finished();
-
flagDESC = !flagDESC;
-
}
-
m_number--;
-
}
-
else
-
{
-
m_number++;
-
if(m_number == 100)
-
{
-
flagDESC = !flagDESC;
-
}
-
}
-
msleep(10);
-
}
-
}
2019/08/27 10:58
说明:
本项目中所使借鉴原型来自:[吉利] 博瑞GE | 仪表HMI设计、吉利汽车HMI项目。
设计图的所有权和解释权都归吉利汽车所有。
本项目所有资源文件均有打不死的小海复刻制作。
本项目代码暂时不会开源,有需要的源码的可与我联系,左上角二维码加微信。
本项目仅限学习交流、禁止商业使用。
文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。
原文链接:dreamlife.blog.csdn.net/article/details/101363501
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)