HMI-14-[Qt 布局]:测速表和油表

简介: 基于Qt的汽车仪表模拟

image.png

点此链接查看视频

程序体验地址:https://pan.baidu.com/s/1rQFZx5JIud8lWdjMHl0tpg

目前版本还没有成功把字体打包的程序中,为了更好的效果体验,请先安装文件中的两个字体

效果图

image.png

车速表,对外的接口

 

/**
     * @brief selfInspection
     * 转速表自检程序
     */
    void selfInspection();
    /**
     * @brief setCarSpeed
     * @param speed
     * 设置车速
     */
    void setCarSpeed(int speed = 0);
    /**
     * @brief setOilMass
     * @param mass
     * 设置油量
     */
    void setOilMass(int mass = 0);

最基本的配置

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_timerCarSpeedSelfInspection = new QTimer(this);
    m_timerOilMassSelfInspection = new QTimer(this);
    connect(m_timerCarSpeedSelfInspection,&QTimer::timeout,this,&Speedmeter::slot_timerCarSpeedSelfInspection);
    connect(m_timerOilMassSelfInspection,&QTimer::timeout,this,&Speedmeter::slot_timerOilMassSelfInspection);
    setCarSpeed();
}

完成后续内容

void Speedmeter::selfInspection()
{
    this->carSpeedSelfInspection();
    this->oilMassSelfInspection();
}
void Speedmeter::setCarSpeed(int speed)
{
    currentCarSpeed = speed;
    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();
}
void Speedmeter::carSpeedSelfInspection()
{
    if(m_timerCarSpeedSelfInspection)
    {
        m_timerCarSpeedSelfInspection->start(8);
    }
}
void Speedmeter::oilMassSelfInspection()
{
    if(m_timerOilMassSelfInspection)
    {
        m_timerOilMassSelfInspection->start(10);
    }
}
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_timerCarSpeedSelfInspection()
{
    if(m_flagCarSpeedSelfInspection)
    {
        if(currentCarSpeed > 0)
        {
            currentCarSpeed = currentCarSpeed - 2;
        }
        else
        {
            m_flagCarSpeedSelfInspection = !m_flagCarSpeedSelfInspection;
            m_timerCarSpeedSelfInspection->stop();
            emit signalSelfInspectionfinished();
        }
    }
    else
    {
        if(currentCarSpeed < 241)
        {
            currentCarSpeed = currentCarSpeed + 2;
        }
        else
        {
            m_flagCarSpeedSelfInspection = !m_flagCarSpeedSelfInspection;
        }
    }
    setCarSpeed(currentCarSpeed);
    emit speedChange(currentCarSpeed);
}
void Speedmeter::slot_timerOilMassSelfInspection()
{
    if(m_flagOilMassSelfInspection)
    {
        if(currentMass>0)
        {
            currentMass--;
        }
        else
        {
            m_flagOilMassSelfInspection = !m_flagOilMassSelfInspection;
            m_timerOilMassSelfInspection->stop();
            emit signalSelfInspectionfinished();
            ui->label_flag_oilmass->setStyleSheet("QLabel{border-image:url(:/Comfort/Resources/MeterDisplay/Comfort/Speedmeter/oilmass_w.png);}");
        }
    }
    else
    {
        if(currentMass <101)
        {
            currentMass++;
        }
        else
        {
            m_flagOilMassSelfInspection = !m_flagOilMassSelfInspection;
        }
    }
    setOilMass(currentMass);
}


目录
相关文章
|
7月前
Qt 布局管理之 堆栈窗体
Qt 布局管理之 堆栈窗体
76 5
|
7月前
Qt 布局管理之 停靠窗口QDockWidget
Qt 布局管理之 停靠窗口QDockWidget
189 0
|
7月前
Qt 布局管理之分割窗口
Qt 布局管理之分割窗口
96 0
|
4月前
【qt】 QGridLayout布局管理器怎么用?
【qt】 QGridLayout布局管理器怎么用?
176 0
|
4月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 空白项Spacer
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 空白项Spacer
197 2
|
4月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
109 2
|
4月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 网格布局Grid Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 网格布局Grid Layout
419 2
|
4月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 水平布局Horizontal Layout
256 2
|
4月前
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 垂直布局Vertical Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 垂直布局Vertical Layout
310 2