欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT仪表盘

程序员文章站 2022-05-26 22:01:33
...

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

效果图如下

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

转速表代码

.h文件

ifndef CONTROL_ENGINE_H

define CONTROL_ENGINE_H

include

include

include

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

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

include

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

}