HMI-20-[刷新问题]解决界面刷新卡顿问题

举报
DreamLife 发表于 2022/04/15 01:09:23 2022/04/15
【摘要】 基于Qt的汽车仪表模拟 先描述现象,就是我的这两个表转速表和速度表,他们按照正常的是同时开始转,完了再同时回到起点,但是呢,我在其他的电脑上实验的时候,发现会有不同步显现,特别明显。 所以,我担心是我的频繁刷新导致的,因为我的刷新频率太高了 每个表里面有两个定时器,定时10毫秒,每个定时器对应的操作里面都有upd...

基于Qt的汽车仪表模拟

先描述现象,就是我的这两个表转速表和速度表,他们按照正常的是同时开始转,完了再同时回到起点,但是呢,我在其他的电脑上实验的时候,发现会有不同步显现,特别明显。

所以,我担心是我的频繁刷新导致的,因为我的刷新频率太高了

每个表里面有两个定时器,定时10毫秒,每个定时器对应的操作里面都有updata这样等于我一个表每秒就要刷新100*100 次,两个表,同时刷新,那就是100*100*100*100 ,着个刷新频率可能真的是太高了,

剩下的呢,就是把原来的仪表的自检函数优化,增加线程,完美解决问题。但是目前的版本还是没有优化的版本,

看下修改后的代码吧,修改之前的改吗可以看前面的内容


  
  
  1. #ifndef TACHOMETER_H
  2. #define TACHOMETER_H
  3. #include <QWidget>
  4. #include <QPainter>
  5. #include <QPaintEvent>
  6. #include <QThread>
  7. class TachoThread : public QThread
  8. {
  9. Q_OBJECT
  10. public:
  11. /**
  12. * @brief run
  13. * 重载线程函数
  14. */
  15. virtual void run();
  16. signals:
  17. void signal_selfInspection(int number);
  18. void signal_finished();
  19. private:
  20. int m_number = 0;
  21. bool flagDESC = false;
  22. };
  23. namespace Ui {
  24. class Tachometer;
  25. }
  26. class Tachometer : public QWidget
  27. {
  28. Q_OBJECT
  29. public:
  30. explicit Tachometer(QWidget *parent = 0);
  31. ~Tachometer();
  32. /**
  33. * @brief selfInspection
  34. * 转速表自检程序
  35. */
  36. void selfInspection();
  37. /**
  38. * @brief setTacho
  39. * @param tacho
  40. * 设置发动机转速0-8000 转速
  41. */
  42. void setTacho(int tacho = 0);
  43. /**
  44. * @brief setTemperature
  45. * @param temperatur
  46. * 设置温度
  47. */
  48. void setTemperature(int temperatur = 0);
  49. signals:
  50. /**
  51. * @brief signalSelfInspection
  52. * 自检完成信号
  53. */
  54. void signalSelfInspectionfinished();
  55. private:
  56. /**
  57. * @brief eventFilter
  58. * @param watched
  59. * @param event
  60. * @return
  61. * 事件过滤器器
  62. */
  63. bool eventFilter(QObject *watched, QEvent *event);
  64. /**
  65. * @brief drawPonter
  66. * @param angle
  67. * 绘制指针
  68. */
  69. void drawPonter(int angle = -135);
  70. /**
  71. * @brief drawPonterShade
  72. * @param angle
  73. * 绘制指针阴影
  74. */
  75. void drawPonterShade(int angle = -135);
  76. /**
  77. * @brief drawTemperature
  78. * @param temperature
  79. * 绘制温度表
  80. */
  81. void drawTemperature(int temperature = 0);
  82. private slots:
  83. /**
  84. * @brief slot_tachoSelfInspection
  85. * @param number
  86. * 完成自检动画
  87. */
  88. void slot_tachoSelfInspection(int number);
  89. public:
  90. int currentTacho = 0; //当前转速
  91. int currentTemperature = 0; //当前温度
  92. private:
  93. Ui::Tachometer *ui;
  94. int m_Angle = 0; //转速表角度
  95. int m_Temperature = 0;
  96. double m_shadeOpacity = 0.0; //指针阴影透明度
  97. TachoThread *m_tachoThread = NULL;
  98. };
  99. #endif // TACHOMETER_H

  
  
  1. #include "tachometer.h"
  2. #include "ui_tachometer.h"
  3. #include <QDebug>
  4. Tachometer::Tachometer(QWidget *parent) :QWidget(parent),ui(new Ui::Tachometer)
  5. {
  6. ui->setupUi(this);
  7. ui->label_mainback->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/mainback.png);}");
  8. ui->label_flag_temperature->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/temperature_r.png);}");
  9. ui->label_pointer->installEventFilter(this);
  10. ui->label_pointer_shade->installEventFilter(this);
  11. ui->label_pointer_temperature->installEventFilter(this);
  12. m_tachoThread = new TachoThread();
  13. connect(m_tachoThread,&TachoThread::signal_finished,this,[=](){m_tachoThread->terminate();});
  14. connect(m_tachoThread,&TachoThread::signal_selfInspection,this,&Tachometer::slot_tachoSelfInspection);
  15. setTacho();
  16. }
  17. Tachometer::~Tachometer()
  18. {
  19. delete ui;
  20. }
  21. void Tachometer::selfInspection()
  22. {
  23. if(m_tachoThread)
  24. {
  25. m_tachoThread->start();
  26. }
  27. }
  28. void Tachometer::setTacho(int tacho)
  29. {
  30. currentTacho = tacho;
  31. m_Angle = currentTacho*0.03375-135;
  32. update();
  33. }
  34. void Tachometer::setTemperature(int temperatur)
  35. {
  36. currentTemperature = temperatur;
  37. m_Temperature = temperatur;
  38. if(m_Temperature >75)
  39. {
  40. ui->label_flag_temperature->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/temperature_r.png);}");
  41. }
  42. else
  43. {
  44. ui->label_flag_temperature->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Tachometer/temperature_w.png);}");
  45. }
  46. update();
  47. }
  48. bool Tachometer::eventFilter(QObject *watched, QEvent *event)
  49. {
  50. if(watched == ui->label_pointer && event->type() == QEvent::Paint)
  51. {
  52. drawPonter(m_Angle);
  53. }
  54. if(watched == ui->label_pointer_shade && event->type() == QEvent::Paint)
  55. {
  56. drawPonterShade(m_Angle);
  57. }
  58. if(watched == ui->label_pointer_temperature && event->type() == QEvent::Paint)
  59. {
  60. drawTemperature(m_Temperature);
  61. }
  62. return QWidget::eventFilter(watched,event);
  63. }
  64. void Tachometer::drawPonter(int angle)
  65. {
  66. m_shadeOpacity = currentTacho / 10000.0;
  67. QPainter painter(ui->label_pointer);
  68. painter.setRenderHint((QPainter::SmoothPixmapTransform));
  69. painter.setRenderHint(QPainter::HighQualityAntialiasing);
  70. painter.translate(304,303.5);
  71. painter.save();
  72. painter.rotate(angle);
  73. painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Tachometer/pointer.png"));
  74. painter.restore();
  75. }
  76. void Tachometer::drawPonterShade(int angle)
  77. {
  78. QPainter painter(ui->label_pointer_shade);
  79. painter.setOpacity(m_shadeOpacity);
  80. painter.setRenderHint((QPainter::SmoothPixmapTransform));
  81. painter.setRenderHint(QPainter::HighQualityAntialiasing);
  82. painter.translate(304,303.5);
  83. painter.save();
  84. painter.rotate(angle);
  85. painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Tachometer/pointer_shade.png"));
  86. painter.restore();
  87. }
  88. void Tachometer::drawTemperature(int temperature)
  89. {
  90. QPainter painter(ui->label_pointer_temperature);
  91. painter.setRenderHint((QPainter::SmoothPixmapTransform));
  92. painter.setRenderHint(QPainter::HighQualityAntialiasing);
  93. painter.translate(304,303.5);
  94. painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
  95. painter.save();
  96. if(temperature<=25)
  97. {
  98. painter.drawArc(-304,-308,608,608,247*16,temperature*7);
  99. }
  100. else if(temperature>25 && temperature <= 50)
  101. {
  102. painter.drawArc(-304,-308,608,608,247*16,24*7);
  103. painter.drawArc(-304,-308,608,608,259*16,(temperature-25)*7);
  104. }
  105. else if(temperature>50 && temperature <= 75)
  106. {
  107. painter.drawArc(-304,-308,608,608,247*16,24*7);
  108. painter.drawArc(-304,-308,608,608,259*16,24*7);
  109. painter.drawArc(-304,-308,608,608,271*16,(temperature-50)*7);
  110. }
  111. else if(temperature>75 && temperature <= 90)
  112. {
  113. painter.drawArc(-304,-308,608,608,247*16,24*7);
  114. painter.drawArc(-304,-308,608,608,259*16,24*7);
  115. painter.drawArc(-304,-308,608,608,271*16,24*7);
  116. painter.drawArc(-304,-308,608,608,283*16,(temperature-75)*7);
  117. }
  118. else if(temperature>90 && temperature <= 100)
  119. {
  120. painter.drawArc(-304,-308,608,608,247*16,24*7);
  121. painter.drawArc(-304,-308,608,608,259*16,24*7);
  122. painter.drawArc(-304,-308,608,608,271*16,24*7);
  123. painter.drawArc(-304,-308,608,608,283*16,15*7);
  124. painter.setPen(QPen(Qt::red,6,Qt::SolidLine));
  125. painter.drawArc(-304,-308,608,608,290*16,(temperature-90)*7);
  126. }
  127. painter.restore();
  128. }
  129. void Tachometer::slot_tachoSelfInspection(int number)
  130. {
  131. currentTacho = number*80;
  132. m_Angle = number*80*0.03375-135;
  133. setTemperature(number);
  134. }
  135. void TachoThread::run()
  136. {
  137. while (true)
  138. {
  139. emit signal_selfInspection(m_number);
  140. if(flagDESC)
  141. {
  142. if(m_number == 0)
  143. {
  144. emit signal_finished();
  145. flagDESC = !flagDESC;
  146. }
  147. m_number--;
  148. }
  149. else
  150. {
  151. m_number++;
  152. if(m_number == 100)
  153. {
  154. flagDESC = !flagDESC;
  155. }
  156. }
  157. msleep(10);
  158. }
  159. }

  
  
  1. #ifndef SPEEDMETER_H
  2. #define SPEEDMETER_H
  3. #include <QWidget>
  4. #include <QPainter>
  5. #include <QPaintEvent>
  6. #include <QThread>
  7. class SpeedThread : public QThread
  8. {
  9. Q_OBJECT
  10. public:
  11. /**
  12. * @brief run
  13. * 重载线程函数
  14. */
  15. virtual void run();
  16. signals:
  17. void signal_selfInspection(int number);
  18. void signal_finished();
  19. private:
  20. int m_number = 0;
  21. bool flagDESC = false;
  22. };
  23. namespace Ui {
  24. class Speedmeter;
  25. }
  26. class Speedmeter : public QWidget
  27. {
  28. Q_OBJECT
  29. public:
  30. explicit Speedmeter(QWidget *parent = 0);
  31. ~Speedmeter();
  32. /**
  33. * @brief selfInspection
  34. * 转速表自检程序
  35. */
  36. void selfInspection();
  37. /**
  38. * @brief setCarSpeed
  39. * @param speed
  40. * 设置车速
  41. */
  42. void setCarSpeed(int speed = 0);
  43. /**
  44. * @brief setOilMass
  45. * @param mass
  46. * 设置油量
  47. */
  48. void setOilMass(int mass = 0);
  49. signals:
  50. /**
  51. * @brief speedChange
  52. * @param speed
  53. * 速度改变信号
  54. */
  55. void speedChange(int speed);
  56. /**
  57. * @brief signalSelfInspection
  58. * 自检完成信号
  59. */
  60. void signalSelfInspectionfinished();
  61. private:
  62. /**
  63. * @brief eventFilter
  64. * @param watched
  65. * @param event
  66. * @return
  67. * 事件过滤器器
  68. */
  69. bool eventFilter(QObject *watched, QEvent *event);
  70. /**
  71. * @brief drawPonter
  72. * @param angle
  73. * 绘制指针
  74. */
  75. void drawPonter(int angle = -135);
  76. /**
  77. * @brief drawPonterShade
  78. * @param angle
  79. * 绘制指针阴影
  80. */
  81. void drawPonterShade(int angle = -135);
  82. /**
  83. * @brief drawOilMass
  84. * @param mess
  85. * 绘制油量表
  86. */
  87. void drawOilMass(int mass = 0);
  88. private slots:
  89. /**
  90. * @brief slot_tachoSelfInspection
  91. * @param number
  92. * 完成自检动画
  93. */
  94. void slot_carSpeedSelfInspection(int number);
  95. public:
  96. int currentCarSpeed = 0; //当前车速
  97. int currentMass = 0; //当前油量
  98. private:
  99. Ui::Speedmeter *ui;
  100. int m_Angle = 0; //速度角度
  101. double m_shadeOpacity = 0.0; //指针阴影透明度
  102. int m_OilMass = 0;
  103. SpeedThread *m_speedThread = NULL;
  104. };
  105. #endif // SPEEDMETER_H

  
  
  1. #include "speedmeter.h"
  2. #include "ui_speedmeter.h"
  3. #include <QDebug>
  4. Speedmeter::Speedmeter(QWidget *parent) : QWidget(parent),ui(new Ui::Speedmeter)
  5. {
  6. ui->setupUi(this);
  7. ui->label_mainback->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/mainback.png);}");
  8. ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_o.png);}");
  9. ui->label_pointer->installEventFilter(this);
  10. ui->label_pointer_shade->installEventFilter(this);
  11. ui->label_oilmass->installEventFilter(this);
  12. m_speedThread = new SpeedThread();
  13. connect(m_speedThread,&SpeedThread::signal_finished,this,[=](){m_speedThread->terminate();});
  14. connect(m_speedThread,&SpeedThread::signal_selfInspection,this,&Speedmeter::slot_carSpeedSelfInspection);
  15. setCarSpeed();
  16. }
  17. Speedmeter::~Speedmeter()
  18. {
  19. delete ui;
  20. }
  21. void Speedmeter::selfInspection()
  22. {
  23. if(m_speedThread)
  24. {
  25. m_speedThread->start();
  26. }
  27. }
  28. void Speedmeter::setCarSpeed(int speed)
  29. {
  30. currentCarSpeed = speed;
  31. emit speedChange(currentCarSpeed);
  32. m_Angle = currentCarSpeed*1.125-135;
  33. update();
  34. }
  35. void Speedmeter::setOilMass(int mass)
  36. {
  37. if(mass<=10)
  38. {
  39. ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_o.png);}");
  40. }
  41. else
  42. {
  43. ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_w.png);}");
  44. }
  45. m_OilMass = mass;
  46. update();
  47. }
  48. bool Speedmeter::eventFilter(QObject *watched, QEvent *event)
  49. {
  50. if(watched == ui->label_pointer && event->type() == QEvent::Paint)
  51. {
  52. drawPonter(m_Angle);
  53. }
  54. if(watched == ui->label_pointer_shade && event->type() == QEvent::Paint)
  55. {
  56. drawPonterShade(m_Angle);
  57. }
  58. if(watched == ui->label_oilmass && event->type() == QEvent::Paint)
  59. {
  60. drawOilMass(m_OilMass);
  61. }
  62. return QWidget::eventFilter(watched,event);
  63. }
  64. void Speedmeter::drawPonter(int angle)
  65. {
  66. m_shadeOpacity = currentCarSpeed / 300.0;
  67. QPainter painter(ui->label_pointer);
  68. painter.setRenderHint((QPainter::SmoothPixmapTransform));
  69. painter.setRenderHint(QPainter::HighQualityAntialiasing);
  70. painter.translate(305,303.5);
  71. painter.save();
  72. painter.rotate(angle);
  73. painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/pointer.png"));
  74. painter.restore();
  75. }
  76. void Speedmeter::drawPonterShade(int angle)
  77. {
  78. QPainter painter(ui->label_pointer_shade);
  79. painter.setOpacity(m_shadeOpacity);
  80. painter.setRenderHint((QPainter::SmoothPixmapTransform));
  81. painter.setRenderHint(QPainter::HighQualityAntialiasing);
  82. painter.translate(305,303.5);
  83. painter.save();
  84. painter.rotate(angle);
  85. painter.drawPixmap(-304,-302,416,500,QPixmap(":/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/pointer_shade.png"));
  86. painter.restore();
  87. }
  88. void Speedmeter::drawOilMass(int mass)
  89. {
  90. QPainter painter(ui->label_oilmass);
  91. painter.setRenderHint((QPainter::SmoothPixmapTransform));
  92. painter.setRenderHint(QPainter::HighQualityAntialiasing);
  93. painter.translate(305,303.5);
  94. painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
  95. painter.save();
  96. if(mass<=10)
  97. {
  98. painter.setPen(QPen(Qt::red,6,Qt::SolidLine));
  99. painter.drawArc(-304,-308,608,608,247*16,mass*7);
  100. }
  101. else if(mass>10 && mass<=25)
  102. {
  103. painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
  104. painter.drawArc(-304,-308,608,608,247*16,mass*7);
  105. }
  106. else if(mass>25 && mass <= 50)
  107. {
  108. painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
  109. painter.drawArc(-304,-308,608,608,247*16,24*7);
  110. painter.drawArc(-304,-308,608,608,259*16,(mass-25)*7);
  111. }
  112. else if(mass>50 && mass <= 75)
  113. {
  114. painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
  115. painter.drawArc(-304,-308,608,608,247*16,24*7);
  116. painter.drawArc(-304,-308,608,608,259*16,24*7);
  117. painter.drawArc(-304,-308,608,608,271*16,(mass-50)*7);
  118. }
  119. else if(mass>75 && mass <= 100)
  120. {
  121. painter.setPen(QPen(Qt::white,6,Qt::SolidLine));
  122. painter.drawArc(-304,-308,608,608,247*16,24*7);
  123. painter.drawArc(-304,-308,608,608,259*16,24*7);
  124. painter.drawArc(-304,-308,608,608,271*16,24*7);
  125. painter.drawArc(-304,-308,608,608,283*16,(mass-75)*7);
  126. }
  127. painter.restore();
  128. }
  129. void Speedmeter::slot_carSpeedSelfInspection(int number)
  130. {
  131. m_OilMass = number;
  132. setCarSpeed(number*2.4);
  133. }
  134. void SpeedThread::run()
  135. {
  136. while (true)
  137. {
  138. emit signal_selfInspection(m_number);
  139. if(flagDESC)
  140. {
  141. if(m_number == 0)
  142. {
  143. emit signal_finished();
  144. flagDESC = !flagDESC;
  145. }
  146. m_number--;
  147. }
  148. else
  149. {
  150. m_number++;
  151. if(m_number == 100)
  152. {
  153. flagDESC = !flagDESC;
  154. }
  155. }
  156. msleep(10);
  157. }
  158. }

2019/08/27 10:58

 

说明:

本项目中所使借鉴原型来自:[吉利] 博瑞GE | 仪表HMI设计吉利汽车HMI项目

设计图的所有权和解释权都归吉利汽车所有。

本项目所有资源文件均有打不死的小海复刻制作。

本项目代码暂时不会开源,有需要的源码的可与我联系,左上角二维码加微信。

本项目仅限学习交流、禁止商业使用。

文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。

原文链接:dreamlife.blog.csdn.net/article/details/101363501

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。