Qt 飞机仪表显示

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

image.png

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

 

转速表代码

.h文件

#ifndef CONTROL_ENGINE_H
#define CONTROL_ENGINE_H
#include <QWidget>
#include <QPainter>
#include <QtMath>
#define PI 3.1415926
#define GREEN 0,255,0
#define YELLOW 211,137,50
#define WHITE 255,255,255
namespace Ui {
class Control_Engine;
}
class Control_Engine : public QWidget
{
    Q_OBJECT
public:
    explicit Control_Engine(QWidget *parent,int temp);
    ~Control_Engine();
    void set_Engine(int temp);
private:
    bool eventFilter(QObject *temp1, QEvent *temp2);
    void draw_Engine();
    void draw_Percent();
private:
    Ui::Control_Engine *ui;
    int num_Engine;
    int num_Percent;
};
#endif // CONTROL_ENGINE_H

cpp文件

#include "control_engine.h"
#include "ui_control_engine.h"
#include <QDebug>
Control_Engine::Control_Engine(QWidget *parent,int temp) :
    QWidget(parent),
    ui(new Ui::Control_Engine)
{
    ui->setupUi(this);
    ui->label_draw->installEventFilter(this);
    ui->label_percent->installEventFilter(this);
    num_Engine = 0;
    num_Percent = 0;
    if(temp == 0)
    {
        ui->label_draw->move(50,0);
        ui->label->move(40,20);
        ui->label_percent->move(0,17);
    }
    else if(temp == 1)
    {
        ui->label_draw->move(0,0);
        ui->label->move(110,20);
        ui->label_percent->move(70,17);
    }
}
Control_Engine::~Control_Engine()
{
    delete ui;
}
void Control_Engine::set_Engine(int temp)
{
    num_Engine = temp;
    num_Percent = temp/2;
    update();
}
bool Control_Engine::eventFilter(QObject *temp1, QEvent *temp2)
{
    if((temp1 == ui->label_draw)&&temp2->type() == QEvent::Paint)
    {
        draw_Engine();
    }
    if((temp1 == ui->label_percent)&&temp2->type() == QEvent::Paint)
    {
        draw_Percent();
    }
}
void Control_Engine::draw_Engine()
{
    if(num_Engine<=200)
    {
        QPainter painter1(ui->label_draw);
        painter1.setRenderHint(QPainter::Antialiasing,true);
        painter1.setPen(QPen(QColor(GREEN),3,Qt::SolidLine,Qt::FlatCap));
        painter1.drawArc(13,3,54,54,180*16,270*16);
        painter1.drawLine(3,30,14,30);
        QPainter painter2(ui->label_draw);
        painter2.setRenderHint(QPainter::Antialiasing,true);
        painter2.setPen(QPen(QColor(GREEN),3,Qt::SolidLine,Qt::FlatCap));
        painter2.drawLine(40,30,(40+27*qSin(num_Engine*PI/180.0)),(30-27*qCos(num_Engine*PI/180.0)));
    }
    else
    {
        QPainter painter1(ui->label_draw);
        painter1.setRenderHint(QPainter::Antialiasing,true);
        painter1.setPen(QPen(QColor(YELLOW),3,Qt::SolidLine,Qt::FlatCap));
        painter1.drawArc(13,3,54,54,180*16,270*16);
        painter1.drawLine(3,30,14,30);
        QPainter painter2(ui->label_draw);
        painter2.setRenderHint(QPainter::Antialiasing,true);
        painter2.setPen(QPen(QColor(YELLOW),3,Qt::SolidLine,Qt::FlatCap));
        painter2.drawLine(40,30,(40+27*qSin(num_Engine*PI/180.0)),(30-27*qCos(num_Engine*PI/180.0)));
    }
}
void Control_Engine::draw_Percent()
{
    if(num_Percent<=100)
    {
        QPainter painter1(ui->label_percent);
        painter1.setPen(QPen(QColor(WHITE),1.5));
        QRect rect1(1,3,36,20);
        painter1.drawRect(rect1);
        QPainter painter2(ui->label_percent);
        painter2.setPen(QPen(QColor(GREEN),1.5));
        QString str = QString::number(num_Percent,10);
        QFont font("宋体",12,QFont::Bold);
        painter2.setFont(font);
        painter2.drawText(rect1,Qt::AlignCenter,str);
    }
    else
    {
        QPainter painter1(ui->label_percent);
        painter1.setPen(QPen(QColor(WHITE),1.5));
        QRect rect1(1,3,36,20);
        painter1.drawRect(rect1);
        QPainter painter2(ui->label_percent);
        painter2.setPen(QPen(QColor(YELLOW),1.5));
        QString str = QString::number(num_Percent,10);
        QFont font("宋体",12,QFont::Bold);
        painter2.setFont(font);
        painter2.drawText(rect1,Qt::AlignCenter,str);
    }
}

百分比代码

.h.

#ifndef CONTROL_PERCENT_H
#define CONTROL_PERCENT_H
#include <QWidget>
#include <QPainter>
#define GREEN 0,255,0
#define WHITE 255,255,255
namespace Ui {
class Control_Percent;
}
class Control_Percent : public QWidget
{
    Q_OBJECT
public:
    explicit Control_Percent(QWidget *parent = 0);
    ~Control_Percent();
    void set_Percent(int temp);
private:
    void draw_Percent();
    bool eventFilter(QObject *temp1, QEvent *temp2);
private:
    Ui::Control_Percent *ui;
    int num_Percent;
};
#endif // CONTROL_PERCENT_H

cpp

#include "control_percent.h"
#include "ui_control_percent.h"
Control_Percent::Control_Percent(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Control_Percent)
{
    ui->setupUi(this);
    num_Percent = 0;
    ui->label_percent->installEventFilter(this);
}
Control_Percent::~Control_Percent()
{
    delete ui;
}
void Control_Percent::set_Percent(int temp)
{
    num_Percent = temp;
}
void Control_Percent::draw_Percent()
{
    QPainter painter1(ui->label_percent);
    painter1.setPen(QPen(QColor(WHITE),1.5));
    QRect rect1(1,3,36,20);
    painter1.drawRect(rect1);
    QPainter painter2(ui->label_percent);
    painter2.setPen(QPen(QColor(GREEN),1.5));
    QString str = QString::number(num_Percent,10);
    QFont font("宋体",12,QFont::Bold);
    painter2.setFont(font);
    painter2.drawText(rect1,Qt::AlignCenter,str);
}
bool Control_Percent::eventFilter(QObject *temp1, QEvent *temp2)
{
    if((temp1 == ui->label_percent)&&temp2->type() == QEvent::Paint)
    {
        draw_Percent();
    }
}

剩下的其它小部分代码

 

 

界面初始化部分代码

void MainWidget::set_Form_down(int temp)
{
    switch (temp) {
    case 0:
    {
        if(ui->widget_Form_Down->isHidden())
        {
            ui->widget_Form_Down->show();
        }
        else
        {
            ui->label_Form_Down_Plane->setStyleSheet("border-image:url(:/Form_Down/Source/Form_Down/F22.png)");
            if(engine_left == NULL)
            {
                engine_left = new Control_Engine(ui->widget_Form_Down,LEFT);
                engine_left->setGeometry(17,20,120,80);
            }
            if(engine_right == NULL)
            {
                engine_right = new Control_Engine(ui->widget_Form_Down,RIGHT);
                engine_right->setGeometry(180,20,120,80);
            }
            //            QPalette palette;
            //            palette.setColor(QPalette::WindowText,Qt::red);
            //            ui->label_ENG->setPalette(palette);
            //            QFont font;
            //            font.setPointSize(12);
            //            ui->label_ENG->setFont(font);
            //            ui->label_ENG->setText("TEST");
            //            ui->label_ENG->setAlignment(Qt::AlignCenter);
            if(percent1 == NULL)
            {
                percent1 = new Control_Percent(ui->widget_Form_Down);
                percent1->setGeometry(30,170,50,26);
                percent1->set_Percent(50);                      //test
            }
            if(percent2 == NULL)
            {
                percent2 = new Control_Percent(ui->widget_Form_Down);
                percent2->setGeometry(245,170,50,26);
                percent2->set_Percent(50);                      //test
            }
            if(percent3 == NULL)
            {
                percent3 = new Control_Percent(ui->widget_Form_Down);
                percent3->setGeometry(30,240,50,26);
                percent3->set_Percent(50);                      //test
            }
            if(percent4 == NULL)
            {
                percent4 = new Control_Percent(ui->widget_Form_Down);
                percent4->setGeometry(245,240,50,26);
                percent4->set_Percent(50);                      //test
            }
        }
        break;
    }
    case 1:
        ui->widget_Form_Down->hide();
        break;
    default:
        break;
    }
}


绘制其它部分代码


void MainWidget::draw_Gun()
{
    QPainter painter1(ui->label_GUN);
    painter1.setPen(QPen(QColor(WHITE),1.5));
    QRect rect1(1,3,76,22);
    painter1.drawRect(rect1);
    QPainter painter2(ui->label_GUN);
    painter2.setPen(QPen(QColor(GREEN),1.5));
    QString str = "GUN "+QString::number(num_GUN,10);
    QFont font("宋体",12,QFont::Bold);
    painter2.setFont(font);
    painter2.drawText(rect1,Qt::AlignCenter,str);
}
void MainWidget::draw_Information()
{
    QPainter painter1(ui->label_Information);
    painter1.setPen(QPen(QColor(WHITE),1.5));
    QRect rect1(1,3,118,45);
    QRect rect2(1,3,118,24);
    QRect rect3(1,24,118,24);
    painter1.drawRect(rect1);
    QPainter painter2(ui->label_Information);
    painter2.setPen(QPen(QColor(WHITE),1.5));
    QString str = "BAY DOORS";
    QFont font("宋体",12,QFont::Bold);
    painter2.setFont(font);
    painter2.drawText(rect2,Qt::AlignCenter,str);
    QPainter painter3(ui->label_Information);
    if(BAY_DOORS == "CLOSED")
    {
        painter3.setPen(QPen(QColor(GREEN),1.5));
    }
    else
    {
        painter3.setPen(QPen(QColor(RED),1.5));
    }
    QFont font1("宋体",12,QFont::Bold);
    painter3.setFont(font1);
    painter3.drawText(rect3,Qt::AlignCenter,BAY_DOORS);
}
void MainWidget::draw_Bay()
{
    QPainter painter(ui->label_BAY);
    QRect rect1(0,0,30,16);
    if(BAY_DOORS == "CLOSED")
    {
        painter.setPen(QPen(QColor(GREEN),1.5));
    }
    else
    {
        painter.setPen(QPen(QColor(RED),1.5));
    }
    QString str = "BAY";
    QFont font("宋体",12,QFont::Bold);
    painter.setFont(font);
    painter.drawText(rect1,Qt::AlignCenter,str);
}

 

目录
相关文章
|
程序员 C语言
Qt编写自定义控件49-飞机仪表盘
一、前言 飞行仪表是测定和表示飞机数据的工具,飞机中必不可少的一部分,飞行员根据飞行仪表表示的数据才能正确地做出判断。一般飞机仪表包括高度表+空速表+垂直速率表+姿态仪+航向指示表+转弯协调表。这次要绘制的是其中的姿势仪,显示飞机相对于地平线的姿态,看姿态仪,飞行员能判断飞机姿态为偏左偏右,及偏上和偏下。
1750 0
|
6月前
|
数据安全/隐私保护 C++ 计算机视觉
Qt(C++)开发一款图片防盗用水印制作小工具
文本水印是一种常用的防盗用手段,可以将文本信息嵌入到图片、视频等文件中,用于识别和证明文件的版权归属。在数字化和网络化的时代,大量的原创作品容易被不法分子盗用或侵犯版权,因此加入文本水印成为了保护原创作品和维护知识产权的必要手段。 通常情况下,文本水印可以包含版权声明、制作者姓名、日期、网址等信息,以帮助识别文件的来源和版权归属。同时,为了增强防盗用效果,文本水印通常会采用字体、颜色、角度等多种组合方式,使得水印难以被删除或篡改,有效地降低了盗用意愿和风险。 开发人员可以使用图像处理技术和编程语言实现文本水印的功能,例如使用Qt的QPainter类进行文本绘制操作,将文本信息嵌入到图片中,
208 1
|
5月前
|
监控 C++ 容器
【qt】MDI多文档界面开发
【qt】MDI多文档界面开发
128 0
|
4月前
|
开发工具 C++
qt开发技巧与三个问题点
本文介绍了三个Qt开发中的常见问题及其解决方法,并提供了一些实用的开发技巧。
101 0
|
4月前
|
5月前
|
C++
C++ Qt开发:QUdpSocket网络通信组件
QUdpSocket是Qt网络编程中一个非常有用的组件,它提供了在UDP协议下进行数据发送和接收的能力。通过简单的方法和信号,可以轻松实现基于UDP的网络通信。不过,需要注意的是,UDP协议本身不保证数据的可靠传输,因此在使用QUdpSocket时,可能需要在应用层实现一些机制来保证数据的完整性和顺序,或者选择在适用的场景下使用UDP协议。
224 2
Qt开发网络嗅探器02
Qt开发网络嗅探器02
|
5月前
|
存储 运维 监控
Qt开发网络嗅探器01
Qt开发网络嗅探器01
|
5月前
|
网络协议 容器
Qt开发网络嗅探器03
Qt开发网络嗅探器03
|
5月前
【qt】多窗口开发
【qt】多窗口开发
79 0