Qt 飞机仪表显示

举报
DreamLife 发表于 2022/04/15 00:44:36 2022/04/15
【摘要】 使用Qt简单谢了一个飞机的一小部分仪表,还没有写完,目前只写了一个界面,不过思想应该是一样的。 效果图如下     其中主要由转速表,和下面的部分数字显示构成   转速表代码 .h文件   #ifndef CONTROL_ENGINE_H#define CONTROL_ENG...

使用Qt简单谢了一个飞机的一小部分仪表,还没有写完,目前只写了一个界面,不过思想应该是一样的。

效果图如下

 

 

其中主要由转速表,和下面的部分数字显示构成

 

转速表代码

.h文件

 


  
  1. #ifndef CONTROL_ENGINE_H
  2. #define CONTROL_ENGINE_H
  3. #include <QWidget>
  4. #include <QPainter>
  5. #include <QtMath>
  6. #define PI 3.1415926
  7. #define GREEN 0,255,0
  8. #define YELLOW 211,137,50
  9. #define WHITE 255,255,255
  10. namespace Ui {
  11. class Control_Engine;
  12. }
  13. class Control_Engine : public QWidget
  14. {
  15. Q_OBJECT
  16. public:
  17. explicit Control_Engine(QWidget *parent,int temp);
  18. ~Control_Engine();
  19. void set_Engine(int temp);
  20. private:
  21. bool eventFilter(QObject *temp1, QEvent *temp2);
  22. void draw_Engine();
  23. void draw_Percent();
  24. private:
  25. Ui::Control_Engine *ui;
  26. int num_Engine;
  27. int num_Percent;
  28. };
  29. #endif // CONTROL_ENGINE_H

 

 

 

 

 

cpp文件

 


  
  1. #include "control_engine.h"
  2. #include "ui_control_engine.h"
  3. #include <QDebug>
  4. Control_Engine::Control_Engine(QWidget *parent,int temp) :
  5. QWidget(parent),
  6. ui(new Ui::Control_Engine)
  7. {
  8. ui->setupUi(this);
  9. ui->label_draw->installEventFilter(this);
  10. ui->label_percent->installEventFilter(this);
  11. num_Engine = 0;
  12. num_Percent = 0;
  13. if(temp == 0)
  14. {
  15. ui->label_draw->move(50,0);
  16. ui->label->move(40,20);
  17. ui->label_percent->move(0,17);
  18. }
  19. else if(temp == 1)
  20. {
  21. ui->label_draw->move(0,0);
  22. ui->label->move(110,20);
  23. ui->label_percent->move(70,17);
  24. }
  25. }
  26. Control_Engine::~Control_Engine()
  27. {
  28. delete ui;
  29. }
  30. void Control_Engine::set_Engine(int temp)
  31. {
  32. num_Engine = temp;
  33. num_Percent = temp/2;
  34. update();
  35. }
  36. bool Control_Engine::eventFilter(QObject *temp1, QEvent *temp2)
  37. {
  38. if((temp1 == ui->label_draw)&&temp2->type() == QEvent::Paint)
  39. {
  40. draw_Engine();
  41. }
  42. if((temp1 == ui->label_percent)&&temp2->type() == QEvent::Paint)
  43. {
  44. draw_Percent();
  45. }
  46. }
  47. void Control_Engine::draw_Engine()
  48. {
  49. if(num_Engine<=200)
  50. {
  51. QPainter painter1(ui->label_draw);
  52. painter1.setRenderHint(QPainter::Antialiasing,true);
  53. painter1.setPen(QPen(QColor(GREEN),3,Qt::SolidLine,Qt::FlatCap));
  54. painter1.drawArc(13,3,54,54,180*16,270*16);
  55. painter1.drawLine(3,30,14,30);
  56. QPainter painter2(ui->label_draw);
  57. painter2.setRenderHint(QPainter::Antialiasing,true);
  58. painter2.setPen(QPen(QColor(GREEN),3,Qt::SolidLine,Qt::FlatCap));
  59. painter2.drawLine(40,30,(40+27*qSin(num_Engine*PI/180.0)),(30-27*qCos(num_Engine*PI/180.0)));
  60. }
  61. else
  62. {
  63. QPainter painter1(ui->label_draw);
  64. painter1.setRenderHint(QPainter::Antialiasing,true);
  65. painter1.setPen(QPen(QColor(YELLOW),3,Qt::SolidLine,Qt::FlatCap));
  66. painter1.drawArc(13,3,54,54,180*16,270*16);
  67. painter1.drawLine(3,30,14,30);
  68. QPainter painter2(ui->label_draw);
  69. painter2.setRenderHint(QPainter::Antialiasing,true);
  70. painter2.setPen(QPen(QColor(YELLOW),3,Qt::SolidLine,Qt::FlatCap));
  71. painter2.drawLine(40,30,(40+27*qSin(num_Engine*PI/180.0)),(30-27*qCos(num_Engine*PI/180.0)));
  72. }
  73. }
  74. void Control_Engine::draw_Percent()
  75. {
  76. if(num_Percent<=100)
  77. {
  78. QPainter painter1(ui->label_percent);
  79. painter1.setPen(QPen(QColor(WHITE),1.5));
  80. QRect rect1(1,3,36,20);
  81. painter1.drawRect(rect1);
  82. QPainter painter2(ui->label_percent);
  83. painter2.setPen(QPen(QColor(GREEN),1.5));
  84. QString str = QString::number(num_Percent,10);
  85. QFont font("宋体",12,QFont::Bold);
  86. painter2.setFont(font);
  87. painter2.drawText(rect1,Qt::AlignCenter,str);
  88. }
  89. else
  90. {
  91. QPainter painter1(ui->label_percent);
  92. painter1.setPen(QPen(QColor(WHITE),1.5));
  93. QRect rect1(1,3,36,20);
  94. painter1.drawRect(rect1);
  95. QPainter painter2(ui->label_percent);
  96. painter2.setPen(QPen(QColor(YELLOW),1.5));
  97. QString str = QString::number(num_Percent,10);
  98. QFont font("宋体",12,QFont::Bold);
  99. painter2.setFont(font);
  100. painter2.drawText(rect1,Qt::AlignCenter,str);
  101. }
  102. }

 

百分比代码

.h.

 


  
  1. #ifndef CONTROL_PERCENT_H
  2. #define CONTROL_PERCENT_H
  3. #include <QWidget>
  4. #include <QPainter>
  5. #define GREEN 0,255,0
  6. #define WHITE 255,255,255
  7. namespace Ui {
  8. class Control_Percent;
  9. }
  10. class Control_Percent : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. explicit Control_Percent(QWidget *parent = 0);
  15. ~Control_Percent();
  16. void set_Percent(int temp);
  17. private:
  18. void draw_Percent();
  19. bool eventFilter(QObject *temp1, QEvent *temp2);
  20. private:
  21. Ui::Control_Percent *ui;
  22. int num_Percent;
  23. };
  24. #endif // CONTROL_PERCENT_H

 

 

 

 

 

cpp

 


  
  1. #include "control_percent.h"
  2. #include "ui_control_percent.h"
  3. Control_Percent::Control_Percent(QWidget *parent) :
  4. QWidget(parent),
  5. ui(new Ui::Control_Percent)
  6. {
  7. ui->setupUi(this);
  8. num_Percent = 0;
  9. ui->label_percent->installEventFilter(this);
  10. }
  11. Control_Percent::~Control_Percent()
  12. {
  13. delete ui;
  14. }
  15. void Control_Percent::set_Percent(int temp)
  16. {
  17. num_Percent = temp;
  18. }
  19. void Control_Percent::draw_Percent()
  20. {
  21. QPainter painter1(ui->label_percent);
  22. painter1.setPen(QPen(QColor(WHITE),1.5));
  23. QRect rect1(1,3,36,20);
  24. painter1.drawRect(rect1);
  25. QPainter painter2(ui->label_percent);
  26. painter2.setPen(QPen(QColor(GREEN),1.5));
  27. QString str = QString::number(num_Percent,10);
  28. QFont font("宋体",12,QFont::Bold);
  29. painter2.setFont(font);
  30. painter2.drawText(rect1,Qt::AlignCenter,str);
  31. }
  32. bool Control_Percent::eventFilter(QObject *temp1, QEvent *temp2)
  33. {
  34. if((temp1 == ui->label_percent)&&temp2->type() == QEvent::Paint)
  35. {
  36. draw_Percent();
  37. }
  38. }

 

 

 

剩下的其它小部分代码

 

 


界面初始化部分代码

 


  
  1. void MainWidget::set_Form_down(int temp)
  2. {
  3. switch (temp) {
  4. case 0:
  5. {
  6. if(ui->widget_Form_Down->isHidden())
  7. {
  8. ui->widget_Form_Down->show();
  9. }
  10. else
  11. {
  12. ui->label_Form_Down_Plane->setStyleSheet("border-image:url(:/Form_Down/Source/Form_Down/F22.png)");
  13. if(engine_left == NULL)
  14. {
  15. engine_left = new Control_Engine(ui->widget_Form_Down,LEFT);
  16. engine_left->setGeometry(17,20,120,80);
  17. }
  18. if(engine_right == NULL)
  19. {
  20. engine_right = new Control_Engine(ui->widget_Form_Down,RIGHT);
  21. engine_right->setGeometry(180,20,120,80);
  22. }
  23. // QPalette palette;
  24. // palette.setColor(QPalette::WindowText,Qt::red);
  25. // ui->label_ENG->setPalette(palette);
  26. // QFont font;
  27. // font.setPointSize(12);
  28. // ui->label_ENG->setFont(font);
  29. // ui->label_ENG->setText("TEST");
  30. // ui->label_ENG->setAlignment(Qt::AlignCenter);
  31. if(percent1 == NULL)
  32. {
  33. percent1 = new Control_Percent(ui->widget_Form_Down);
  34. percent1->setGeometry(30,170,50,26);
  35. percent1->set_Percent(50); //test
  36. }
  37. if(percent2 == NULL)
  38. {
  39. percent2 = new Control_Percent(ui->widget_Form_Down);
  40. percent2->setGeometry(245,170,50,26);
  41. percent2->set_Percent(50); //test
  42. }
  43. if(percent3 == NULL)
  44. {
  45. percent3 = new Control_Percent(ui->widget_Form_Down);
  46. percent3->setGeometry(30,240,50,26);
  47. percent3->set_Percent(50); //test
  48. }
  49. if(percent4 == NULL)
  50. {
  51. percent4 = new Control_Percent(ui->widget_Form_Down);
  52. percent4->setGeometry(245,240,50,26);
  53. percent4->set_Percent(50); //test
  54. }
  55. }
  56. break;
  57. }
  58. case 1:
  59. ui->widget_Form_Down->hide();
  60. break;
  61. default:
  62. break;
  63. }
  64. }

绘制其它部分代码


  
  1. void MainWidget::draw_Gun()
  2. {
  3. QPainter painter1(ui->label_GUN);
  4. painter1.setPen(QPen(QColor(WHITE),1.5));
  5. QRect rect1(1,3,76,22);
  6. painter1.drawRect(rect1);
  7. QPainter painter2(ui->label_GUN);
  8. painter2.setPen(QPen(QColor(GREEN),1.5));
  9. QString str = "GUN "+QString::number(num_GUN,10);
  10. QFont font("宋体",12,QFont::Bold);
  11. painter2.setFont(font);
  12. painter2.drawText(rect1,Qt::AlignCenter,str);
  13. }
  14. void MainWidget::draw_Information()
  15. {
  16. QPainter painter1(ui->label_Information);
  17. painter1.setPen(QPen(QColor(WHITE),1.5));
  18. QRect rect1(1,3,118,45);
  19. QRect rect2(1,3,118,24);
  20. QRect rect3(1,24,118,24);
  21. painter1.drawRect(rect1);
  22. QPainter painter2(ui->label_Information);
  23. painter2.setPen(QPen(QColor(WHITE),1.5));
  24. QString str = "BAY DOORS";
  25. QFont font("宋体",12,QFont::Bold);
  26. painter2.setFont(font);
  27. painter2.drawText(rect2,Qt::AlignCenter,str);
  28. QPainter painter3(ui->label_Information);
  29. if(BAY_DOORS == "CLOSED")
  30. {
  31. painter3.setPen(QPen(QColor(GREEN),1.5));
  32. }
  33. else
  34. {
  35. painter3.setPen(QPen(QColor(RED),1.5));
  36. }
  37. QFont font1("宋体",12,QFont::Bold);
  38. painter3.setFont(font1);
  39. painter3.drawText(rect3,Qt::AlignCenter,BAY_DOORS);
  40. }
  41. void MainWidget::draw_Bay()
  42. {
  43. QPainter painter(ui->label_BAY);
  44. QRect rect1(0,0,30,16);
  45. if(BAY_DOORS == "CLOSED")
  46. {
  47. painter.setPen(QPen(QColor(GREEN),1.5));
  48. }
  49. else
  50. {
  51. painter.setPen(QPen(QColor(RED),1.5));
  52. }
  53. QString str = "BAY";
  54. QFont font("宋体",12,QFont::Bold);
  55. painter.setFont(font);
  56. painter.drawText(rect1,Qt::AlignCenter,str);
  57. }

 

 

 

 

 

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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