Qt做警告处理界面

解决的问题:

做上位机时,多有检测仪器状态,事实显示警告,错误等状态,笔者就是需要显示各种仪器状态,做显示,后做出处理逻辑

Axure设计图:


需求:更新状态,根据状态值给出处理逻辑,主要看界面;具体业务代码具体实现

如何实现:

实现方式:QWidget中进行布局, QLabel控件进行画图等,实现的组件单独效果如下

可以进行复用,所以暂且叫它:WarningComponent


代码块解析:

.h文件:

cpp 复制代码
/*********************************************************************************
*Author:zhouFuLiang
*Date: 2023-08-03  11:51
*Description: 警告组件界面
**********************************************************************************/
#ifndef WARNINGCOMPONENT_H
#define WARNINGCOMPONENT_H

#include <QWidget>
#include <QLabel>

class WarningComponent : public QWidget
{
    Q_OBJECT

public:
    WarningComponent(const QString &strTitle, const QString &strImgPath, QWidget *parent = 0);
    ~WarningComponent();
    /*
     * @brief 设置当前状态
     * @param str 状态
     * @param 唯一对外的接口
     * @return
    */
    void setCurrentState(const QString &str);
protected:
    void paintEvent(QPaintEvent *event);

private:
    void initObject();
    void initGeometry();
    void initConnect();

private:
    QWidget *m_pTitleWidget;
    QLabel *m_pTitleLabel;
    QLabel *m_pImageLabel;
    QLabel *m_pStateLabel;
    QString m_strTitle;
    QString m_strImgPath;
};

#endif // WARNINGCOMPONENT_H

.cpp文件

cpp 复制代码
#include "warningcomponent.h"
#include "public.h"

WarningComponent::WarningComponent(const QString &strTitle, const QString &strImgPath, QWidget *parent)
    : QWidget(parent)
    ,m_strTitle(strTitle)
    ,m_strImgPath(strImgPath)
{
    initObject();
    initGeometry();
    initConnect();
}

WarningComponent::~WarningComponent()
{

}

void WarningComponent::setCurrentState(const QString &str)
{
    m_pStateLabel->setText(str);
}

void WarningComponent::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    //    painter.setBrush(QBrush(QColor(153, 204, 204)));
    //    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    painter.drawRoundedRect(rect, 15, 15);
    QWidget::paintEvent(event);
}

void WarningComponent::initObject()
{
    this->setWindowFlags(Qt::FramelessWindowHint);

    m_pTitleWidget = new QWidget(this);
    m_pTitleLabel = new QLabel(m_strTitle, m_pTitleWidget);
    m_pTitleLabel->setStyleSheet("QLabel{color:white;}");
    m_pTitleWidget->setStyleSheet("background-color:#717E98; border-top-left-radius:5px; border-top-right-radius:5px;");
    m_pImageLabel = new QLabel(this);
    m_pImageLabel->setPixmap(QPixmap(m_strImgPath));
    m_pImageLabel->resize(100,200);
//    m_pImageLabel->setScaledContents(true);
    m_pStateLabel = new QLabel("State...", this);
}

void WarningComponent::initGeometry()
{
    QVBoxLayout *layout1 = new QVBoxLayout;
    layout1->addWidget(m_pTitleLabel);
    m_pTitleWidget->setLayout(layout1);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addWidget(new QLabel(QString::fromLocal8Bit("当前状态"), this), 0, Qt::AlignLeft);
    vLayout->addSpacing(30);
    vLayout->addWidget(m_pStateLabel, 0, Qt::AlignCenter);
    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(m_pImageLabel);
    hLayout->addLayout(vLayout);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(m_pTitleWidget);
    layout->addStretch();
    layout->addSpacing(10);
    layout->addLayout(hLayout);
    layout->addStretch();
    this->setLayout(layout);
}

void WarningComponent::initConnect()
{

}
相关推荐
Shartin3 分钟前
Can201-Introduction to Networking: Application Layer应用层
服务器·开发语言·php
共享家95271 小时前
linux_线程概念
linux·开发语言·jvm
apihz1 小时前
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
android·服务器·开发语言·网络·数据库·网络协议·tcp/ip
tanyongxi662 小时前
C++ Map 和 Set 详解:从原理到实战应用
开发语言·c++
飒飒真编程2 小时前
C++类模板继承部分知识及测试代码
开发语言·c++·算法
博睿谷IT99_3 小时前
华为数据通信网络基础
开发语言·华为·php·华为认证
蓝桉(努力版)3 小时前
MATLAB可视化5:华夫图(饼图的平替可以表示种类的分布,附有完整代码详细讲解)(求个关注、点赞和收藏)(对配色不满意可以自己调节配色,附调色教程)
开发语言·数学建模·matlab·信息可视化·matlab可视化
艾莉丝努力练剑4 小时前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(五)
c语言·开发语言·数据结构·学习·算法
云空4 小时前
《QtPy:Python与Qt的完美桥梁》
开发语言·python·qt·pyqt
晓13134 小时前
JavaScript加强篇——第六章 定时器(延时函数)与JS执行机制
开发语言·javascript·ecmascript