在上位机系统中,经常需要显示当前软件版本,当前fpga版本,那么咱就做一个help的菜单的关于界面
解决的问题:
解决显示版本信息的UI
Axure界面图:
如何实现:
实现文件的存储:QSetting来存储版本信息文件,详细看代码
UI的实现图:
代码块解析:
.h文件
/*********************************************************************************
*Author:zhouFuLiang
*Date: 2024-04-20 16:46
*Description: 显示关于信息
**********************************************************************************/
#ifndef DEABOUTUI_H
#define DEABOUTUI_H
#include <QDialog>
#include <QLabel>
// 数据结构,用于存储版本信息
struct VersionInfo {
QString appVersion;
QString installDate;
QString content;
QString fpgaVersion;
QString fpgaInstDate;
QString fpgaContent;
};
class DeAboutUi : public QDialog
{
Q_OBJECT
public:
explicit DeAboutUi(QWidget *parent = 0);
void writeFpgaVesion(int iValue);
void writeFpgaInstallDate(int iValue);
signals:
public slots:
private:
void initConnect();
void initGeometry();
/*
* @brief 将整数转换为日期时间,
* @param intValue 如2022
* @param
* @return 返回对应的QString
*/
QString intToDateString(int intValue);
void writeDefaultVersionInfo();
VersionInfo readVersionInfo();
/*
* @brief 更新版本信息的UI
* @param
* @param
* @return
*/
void notifyVersionUi();
private:
QLabel *m_pSoftVer;
QLabel *m_pSoftData;
QLabel *m_pSoftContent;
QLabel *m_pFpgaVer;
QLabel *m_pFpgaData;
QLabel *m_pFpgaContent;
QFrame *pVLine1;
QFrame *pVLine2;
QString m_sysVesPath;
};
#endif // DEABOUTUI_H
.cpp文件
#include "deabout_ui.h"
#include <QVBoxLayout>
#include <QGridLayout>
#include "public.h"
DeAboutUi::DeAboutUi(QWidget *parent) : QDialog(parent)
{
this->resize(350,250);
this->setWindowTitle(QString::fromLocal8Bit("关于"));
m_pSoftVer = new QLabel(this);
m_pSoftData= new QLabel(this);
m_pSoftContent= new QLabel(this);
m_pFpgaVer= new QLabel(this);
m_pFpgaData= new QLabel(this);
m_pFpgaContent= new QLabel(this);
pVLine1 = new QFrame(this);
pVLine1->setFrameStyle(QFrame::HLine | QFrame::Plain);
pVLine1->setLineWidth(2);
pVLine1->setStyleSheet("color: black");
pVLine2 = new QFrame(this);
pVLine2->setFrameStyle(QFrame::HLine | QFrame::Plain);
pVLine2->setLineWidth(2);
pVLine2->setStyleSheet("color: black");
initGeometry();
initConnect();
m_sysVesPath = qApp->applicationDirPath()+ "/" + "AppDoc/SystemIni/" + "version.ini";
// 检查配置文件是否存在
QFileInfo configFile(m_sysVesPath);
if (!configFile.exists()) {
// 如果配置文件不存在,则写入默认版本信息
writeDefaultVersionInfo();
}
// 读取版本信息
notifyVersionUi();
writeFpgaInstallDate(20210212);
}
void DeAboutUi::writeFpgaVesion(int iValue)
{
QString fpgaVersion= QString::number(iValue);
// 创建QSettings对象并指定INI文件路径
QSettings settings(m_sysVesPath, QSettings::IniFormat);
// 写入版本信息到INI文件
settings.beginGroup("FPGAVersion");
settings.setValue("FpgaVersion", fpgaVersion);
settings.endGroup();
notifyVersionUi();
}
void DeAboutUi::writeFpgaInstallDate(int iValue)
{
QString fpgaDate= intToDateString(iValue);
// 创建QSettings对象并指定INI文件路径
QSettings settings(m_sysVesPath, QSettings::IniFormat);
// 写入版本信息到INI文件
settings.beginGroup("FPGAVersion");
settings.setValue("FpgaDate", fpgaDate);
settings.endGroup();
notifyVersionUi();
}
void DeAboutUi::initConnect()
{
}
void DeAboutUi::initGeometry()
{
QFont font;
font.setBold(true);
font.setFamily("Microsoft JhengHei");
QVBoxLayout *layout = new QVBoxLayout;
QLabel *sLabey = new QLabel(QString::fromLocal8Bit("软件版本:"), this);
sLabey->setFont(font);
layout->addWidget(sLabey, Qt::AlignLeft | Qt::AlignBottom);
QGridLayout *glayout1 = new QGridLayout;
glayout1->addWidget(new QLabel(QString::fromLocal8Bit("版本号:"), this),0, 0, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout1->addWidget(m_pSoftVer,0, 1, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout1->addWidget(new QLabel(QString::fromLocal8Bit("日期:"), this),1, 0, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout1->addWidget(m_pSoftData,1, 1, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout1->addWidget(new QLabel(QString::fromLocal8Bit("内容:"), this),2, 0, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout1->addWidget(m_pSoftContent,2, 1, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
QGridLayout *glayout2 = new QGridLayout;
glayout2->addWidget(new QLabel(QString::fromLocal8Bit("版本号:"), this),0, 0, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout2->addWidget(m_pFpgaVer,0, 1, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout2->addWidget(new QLabel(QString::fromLocal8Bit("日期:"), this),1, 0, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout2->addWidget(m_pFpgaData,1, 1, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout2->addWidget(new QLabel(QString::fromLocal8Bit("内容:"), this),2, 0, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
glayout2->addWidget(m_pFpgaContent,2, 1, 1, 1,Qt::AlignLeft | Qt::AlignHCenter);
layout->addWidget(pVLine1, 2, Qt::AlignTop);
layout->addLayout(glayout1);
QLabel *fLabey = new QLabel(QString::fromLocal8Bit("FPGA版本:"), this);
fLabey->setFont(font);
layout->addWidget(fLabey, Qt::AlignLeft | Qt::AlignBottom);
layout->addWidget(pVLine2, 0, Qt::AlignTop);
layout->addLayout(glayout2);
this->setLayout(layout);
}
QString DeAboutUi::intToDateString(int intValue)
{
// 解析年、月、日
int year = intValue / 10000; // 获取年份
int month = (intValue / 100) % 100; // 获取月份
int day = intValue % 100; // 获取日
// 构建QDateTime对象
QDateTime dateTime(QDate(year, month, day));
// 将QDateTime对象格式化为字符串
return dateTime.toString("yyyy-MM-dd"); // 根据需要选择日期格式
}
void DeAboutUi::writeDefaultVersionInfo()
{
// 创建QSettings对象并指定INI文件路径
QSettings settings(m_sysVesPath, QSettings::IniFormat);
// 写入默认版本信息到INI文件
settings.beginGroup("APPVersion");
settings.setValue("AppVersion", QString("%1").arg(FORMAL_VERSION_NUMBER));
settings.setValue("AppDate", "2024-04-20");
settings.setValue("AppContent", "strContent");
settings.endGroup();
settings.beginGroup("FPGAVersion");
settings.setValue("FpgaVersion", QString("%1").arg(FORMAL_VERSION_NUMBER));
settings.setValue("FpgaDate", "2024-04-20");
settings.setValue("FpgaContent", "strContent");
settings.endGroup();
qDebug() << "Default version information has been written to config.ini";
}
VersionInfo DeAboutUi::readVersionInfo()
{
// 创建QSettings对象并指定INI文件路径
QSettings settings(m_sysVesPath, QSettings::IniFormat);
// 读取版本信息
VersionInfo info;
settings.beginGroup("APPVersion");
info.appVersion = settings.value("AppVersion").toString();
info.installDate = settings.value("AppDate").toString();
info.content = settings.value("AppContent").toString();
settings.endGroup();
settings.beginGroup("FPGAVersion");
info.fpgaVersion = settings.value("FpgaVersion").toString();
info.fpgaInstDate = settings.value("FpgaDate").toString();
info.fpgaContent = settings.value("FpgaContent").toString();
settings.endGroup();
return info;
}
void DeAboutUi::notifyVersionUi()
{
VersionInfo vers = readVersionInfo();
m_pSoftVer->setText(QString("V%1").arg(vers.appVersion));
m_pSoftData->setText(QString("%1").arg(vers.installDate));
m_pSoftContent->setText(QString("%1").arg(vers.content));
m_pFpgaVer->setText(QString("%1").arg(vers.fpgaVersion));
m_pFpgaData->setText(QString("%1").arg(vers.fpgaInstDate));
m_pFpgaContent->setText(QString("%1").arg(vers.fpgaContent));
}
其中有两点:fpga推给上位机的数据是一个int类型的如 int date = 20210212;需要转换文章来源:https://www.toymoban.com/news/detail-857964.html
还有QSetting的用法,是为了单独去改变其中的一个值,写入文件,后期读取,如果FPga有推送版本,就更新对应的值,然后用UI的update,函数,来更新UI的改变。文章来源地址https://www.toymoban.com/news/detail-857964.html
到了这里,关于Qt做关于界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!