调整菜单栏

This commit is contained in:
zzh 2025-09-15 10:16:01 +08:00
parent 5445380824
commit 6c7499efa4
2 changed files with 404 additions and 268 deletions

View File

@ -58,6 +58,42 @@ MainWindow::~MainWindow()
{
}
/*
void MainWindow::setupMenuBar()
{
// 获取主窗口的菜单栏
QMenuBar *mainMenuBar = this->menuBar();
// 创建帮助菜单
QMenu *helpMenu = mainMenuBar->addMenu("帮助(&H)");
// 创建版本更新说明菜单项
versionUpdateAction = new QAction("版本更新说明(&V)", this);
helpMenu->addAction(versionUpdateAction);
// 连接信号槽
connect(versionUpdateAction, &QAction::triggered, this, &MainWindow::showVersionUpdateInfo);
}
void MainWindow::showVersionUpdateInfo()
{
QString versionInfo =
"版本更新说明\n\n"
"版本 1.3.0 (2024-01-15)\n"
"• 新增版本更新说明菜单\n"
"• 优化MQTT连接稳定性\n"
"• 修复已知问题\n\n"
"版本 1.2.0 (2023-12-20)\n"
"• 改进用户界面\n"
"• 增强数据处理能力\n\n"
"版本 1.1.0 (2023-11-10)\n"
"• 初始版本发布\n"
"• 基础功能实现";
QMessageBox::information(this, "版本更新说明", versionInfo);
}
*/
void MainWindow::setupUI() {
setWindowTitle("兔喜Test1.2 Author:Zhangzhenghao Email:zzh9953477@gmail.com");
@ -94,19 +130,26 @@ void MainWindow::setupUI() {
mainLayout->setSpacing(10); // 减少布局间距
mainLayout->setContentsMargins(15, 10, 15, 10); // 减少边距
// 获取适合当前主题的文字颜色
QString textColor = getTextColorForTheme();
// MQTT连接区域
connectionGroup = new QGroupBox("MQTT连接设置", this);
connectionGroup->setStyleSheet("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; }");
connectionGroup->setStyleSheet(QString("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; color: %1; }").arg(textColor));
QVBoxLayout *connectionLayout = new QVBoxLayout(connectionGroup);
connectionLayout->setSpacing(10);
// 服务器和端口 - 使用水平布局
QHBoxLayout *serverLayout = new QHBoxLayout();
serverLayout->addWidget(new QLabel("服务器:"));
QLabel *serverLabel = new QLabel("服务器:");
serverLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
serverLayout->addWidget(serverLabel);
brokerEdit = new QLineEdit("tx-mqtt.zt-express.com");
brokerEdit->setMinimumHeight(30);
serverLayout->addWidget(brokerEdit);
serverLayout->addWidget(new QLabel("端口:"));
QLabel *portLabel = new QLabel("端口:");
portLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
serverLayout->addWidget(portLabel);
portEdit = new QLineEdit("1883");
portEdit->setMinimumHeight(30);
portEdit->setMaximumWidth(100);
@ -115,11 +158,15 @@ void MainWindow::setupUI() {
// 用户名和密码
QHBoxLayout *authLayout = new QHBoxLayout();
authLayout->addWidget(new QLabel("用户名:"));
QLabel *usernameLabel = new QLabel("用户名:");
usernameLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
authLayout->addWidget(usernameLabel);
usernameEdit = new QLineEdit("TJ251679787196");
usernameEdit->setMinimumHeight(30);
authLayout->addWidget(usernameEdit);
authLayout->addWidget(new QLabel("密码:"));
QLabel *passwordLabel = new QLabel("密码:");
passwordLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
authLayout->addWidget(passwordLabel);
passwordEdit = new QLineEdit();
passwordEdit->setEchoMode(QLineEdit::Password);
passwordEdit->setMinimumHeight(30);
@ -146,7 +193,9 @@ void MainWindow::setupUI() {
deviceGroup->setStyleSheet("QGroupBox { font-weight: bold; font-size: 12px; padding-top: 10px; }");
QHBoxLayout *deviceLayout = new QHBoxLayout(deviceGroup);
deviceLayout->setSpacing(10);
deviceLayout->addWidget(new QLabel("需要测试的设备SN:"));
QLabel *deviceSnLabel = new QLabel("需要测试的设备SN:");
deviceSnLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
deviceLayout->addWidget(deviceSnLabel);
deviceSnEdit = new QLineEdit("TJ251617198122");
deviceSnEdit->setMinimumHeight(30);
deviceLayout->addWidget(deviceSnEdit);
@ -161,14 +210,18 @@ void MainWindow::setupUI() {
// 第一行:颜色和闪烁
QHBoxLayout *row1Layout = new QHBoxLayout();
row1Layout->addWidget(new QLabel("颜色:"));
QLabel *colorLabel = new QLabel("颜色:");
colorLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
row1Layout->addWidget(colorLabel);
colorCombo = new QComboBox();
colorCombo->addItems({"8-灭", "1-红", "2-黄", "3-蓝", "4-绿", "5-青", "6-白", "7-紫"});
colorCombo->setCurrentIndex(6);
colorCombo->setMinimumHeight(30);
row1Layout->addWidget(colorCombo);
row1Layout->addWidget(new QLabel("闪烁:"));
QLabel *flashLabel = new QLabel("闪烁:");
flashLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
row1Layout->addWidget(flashLabel);
flashCombo = new QComboBox();
flashCombo->addItems({"0-关闭", "1-开启"});
flashCombo->setCurrentIndex(0);
@ -179,14 +232,18 @@ void MainWindow::setupUI() {
// 第二行:闪烁间隔和点亮时长
QHBoxLayout *row2Layout = new QHBoxLayout();
row2Layout->addWidget(new QLabel("闪烁间隔(秒):"));
QLabel *flashIntervalLabel = new QLabel("闪烁间隔(秒):");
flashIntervalLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
row2Layout->addWidget(flashIntervalLabel);
flashIntervalSpin = new QSpinBox();
flashIntervalSpin->setRange(1, 60);
flashIntervalSpin->setValue(4);
flashIntervalSpin->setMinimumHeight(30);
row2Layout->addWidget(flashIntervalSpin);
row2Layout->addWidget(new QLabel("点亮时长(秒):"));
QLabel *lightDurationLabel = new QLabel("点亮时长(秒):");
lightDurationLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
row2Layout->addWidget(lightDurationLabel);
lightDurationSpin = new QSpinBox();
lightDurationSpin->setRange(1, 300);
lightDurationSpin->setValue(30);
@ -197,7 +254,9 @@ void MainWindow::setupUI() {
// 第三行:声音和发送按钮
QHBoxLayout *row3Layout = new QHBoxLayout();
row3Layout->addWidget(new QLabel("声音:"));
QLabel *soundLabel = new QLabel("声音:");
soundLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
row3Layout->addWidget(soundLabel);
soundCombo = new QComboBox();
soundCombo->addItems({"0-关闭", "1-开启"});
soundCombo->setCurrentIndex(0);
@ -206,7 +265,7 @@ void MainWindow::setupUI() {
row3Layout->addStretch();
sendLightAllBtn = new QPushButton("发送全部点亮");
sendLightAllBtn->setMinimumHeight(40);
sendLightAllBtn->setMinimumHeight(35);
sendLightAllBtn->setMinimumWidth(120);
sendLightAllBtn->setStyleSheet("QPushButton { background-color: #4CAF50; color: white; font-weight: bold; padding: 10px 20px; border-radius: 6px; font-size: 14px; }");
row3Layout->addWidget(sendLightAllBtn);
@ -225,7 +284,7 @@ void MainWindow::setupUI() {
" border: 2px solid #ddd; "
" border-radius: 8px; "
"}"
);
);
snGroup->setMinimumHeight(120); // 减少最小高度
snGroup->setMaximumHeight(140); // 设置最大高度限制
@ -249,7 +308,7 @@ void MainWindow::setupUI() {
" border-radius: 6px; "
" min-height: 12px; "
"}"
);
);
snHeaderLayout->addWidget(snCountLabel);
snHeaderLayout->addStretch();
@ -269,7 +328,7 @@ void MainWindow::setupUI() {
"QPushButton:hover { "
" background-color: #1976D2; "
"}"
);
);
snHeaderLayout->addWidget(searchLightStripBtn);
snLayout->addLayout(snHeaderLayout);
@ -293,7 +352,7 @@ void MainWindow::setupUI() {
"QPushButton:pressed { "
" background-color: #1565C0; "
"}"
);
);
connect(openManagerBtn, &QPushButton::clicked, this, &MainWindow::openLightStripManager);
snLayout->addWidget(openManagerBtn);
@ -318,7 +377,9 @@ void MainWindow::setupUI() {
// 当前版本显示
QHBoxLayout *versionLayout = new QHBoxLayout();
versionLayout->addWidget(new QLabel("当前版本:"));
QLabel *versionLabel = new QLabel("当前版本:");
versionLabel->setStyleSheet(QString("QLabel { color: %1; }").arg(textColor));
versionLayout->addWidget(versionLabel);
currentVersionEdit = new QLineEdit(this);
currentVersionEdit->setReadOnly(true);
versionLayout->addWidget(currentVersionEdit);
@ -346,6 +407,9 @@ void MainWindow::setupUI() {
otaLayout->addWidget(otaStatusLabel);
mainLayout->addWidget(otaGroup);
// 在setupUI的最后调用菜单创建
createMenus();
}
void MainWindow::onConnectClicked() {
@ -394,13 +458,13 @@ void MainWindow::onSendLightAllClicked()
if (deviceSn.isEmpty()) {
messageDisplay->append(QString("[%1] 错误: 请输入设备SN")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
return;
}
if (!mqttClient->isConnected()) {
messageDisplay->append(QString("[%1] 错误: MQTT未连接")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
return;
}
@ -437,18 +501,18 @@ void MainWindow::onSendLightAllClicked()
if (mqttClient->publish(topic, message)) {
messageDisplay->append(QString("[%1] 发送全部点亮指令到设备 %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(deviceSn));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(deviceSn));
messageDisplay->append(QString("主题: %1").arg(topic));
messageDisplay->append(QString("参数: 颜色=%1, 闪烁=%2, 间隔=%3s, 时长=%4s, 声音=%5")
.arg(colorCombo->currentText())
.arg(flashCombo->currentText())
.arg(flashInterval)
.arg(lightDuration)
.arg(soundCombo->currentText()));
.arg(colorCombo->currentText())
.arg(flashCombo->currentText())
.arg(flashInterval)
.arg(lightDuration)
.arg(soundCombo->currentText()));
} else {
messageDisplay->append(QString("[%1] 发送全部点亮指令失败")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
}
}
@ -478,35 +542,35 @@ void MainWindow::onMqttConnected() {
mqttClient->subscribe(resourceReportTopic);
messageDisplay->append(QString("[%1] 已订阅主题: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(responseTopic));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(responseTopic));
messageDisplay->append(QString("[%1] 已订阅主题: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(stationTopic));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(stationTopic));
messageDisplay->append(QString("[%1] 已订阅主题: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(lightReportTopic));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(lightReportTopic));
messageDisplay->append(QString("[%1] 已订阅主题: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(adviceDeviceTopic));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(adviceDeviceTopic));
messageDisplay->append(QString("[%1] 已订阅主题: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(resourceReportTopic));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(resourceReportTopic));
}
}
void MainWindow::onMqttDisconnected()
{
messageDisplay->append(QString("[%1] MQTT连接断开")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
updateConnectionStatus(false);
}
void MainWindow::onMqttError(const QString &error)
{
messageDisplay->append(QString("[%1] MQTT错误: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(error));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(error));
updateConnectionStatus(false);
}
@ -557,8 +621,8 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message)
if (error.error != QJsonParseError::NoError) {
messageDisplay->append(QString("[%1] JSON解析错误: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(error.errorString()));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(error.errorString()));
return;
}
@ -591,18 +655,18 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message)
if (dataObj.contains("baseVersion")) {
QString version = dataObj["baseVersion"].toString();
messageDisplay->append(QString("[%1] 找到baseVersion: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
// 确保currentVersionEdit存在
if (currentVersionEdit) {
currentVersionEdit->setText(version);
messageDisplay->append(QString("[%1] 版本已设置到输入框: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
} else {
messageDisplay->append(QString("[%1] 错误: currentVersionEdit为空")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
}
statusLabel->setText(QString("获取到当前版本: %1").arg(version));
@ -616,14 +680,14 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message)
QString resource = root["resource"].toString();
QString version = resource.trimmed();
messageDisplay->append(QString("[%1] 找到resource字段: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
if (currentVersionEdit) {
currentVersionEdit->setText(version);
messageDisplay->append(QString("[%1] 版本已设置到输入框: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(version));
}
statusLabel->setText(QString("获取到当前版本: %1").arg(version));
return;
@ -639,12 +703,12 @@ void MainWindow::onMessageReceived(const QString &topic, const QString &message)
if (otaOperationPending && (realTopic.contains("/station/report") || realTopic.contains("/resource/report"))) {
QString operationType = isUpgradeOperation ? "升级" : "降级";
QString successMessage = QString("OTA%1成功 版本: %2")
.arg(operationType)
.arg(pendingOtaVersion);
.arg(operationType)
.arg(pendingOtaVersion);
messageDisplay->append(QString("[%1] %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(successMessage));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(successMessage));
// 重置OTA状态
otaOperationPending = false;
@ -712,8 +776,8 @@ void MainWindow::onOtaUpgradeClicked() {
// 显示发送的消息内容
QMessageBox::information(this, "OTA升级命令",
QString("已发送升级命令:\n设备SN: %1\n版本: 1.1.41\n主题: %2\n消息: %3")
.arg(deviceSn, topic, message));
QString("已发送升级命令:\n设备SN: %1\n版本: 1.1.41\n主题: %2\n消息: %3")
.arg(deviceSn, topic, message));
} else {
statusLabel->setText("MQTT未连接无法发送OTA命令");
// 如果发送失败,重置状态
@ -768,8 +832,8 @@ void MainWindow::onOtaDowngradeClicked() {
// 显示发送的消息内容
QMessageBox::information(this, "OTA降级命令",
QString("已发送降级命令:\n设备SN: %1\n版本: 1.1.40\n主题: %2\n消息: %3")
.arg(deviceSn, topic, message));
QString("已发送降级命令:\n设备SN: %1\n版本: 1.1.40\n主题: %2\n消息: %3")
.arg(deviceSn, topic, message));
} else {
statusLabel->setText("MQTT未连接无法发送OTA命令");
// 如果发送失败,重置状态
@ -785,7 +849,7 @@ void MainWindow::sendSearchLightStripCommand() {
QString deviceSn = deviceSnEdit->text().trimmed();
if (deviceSn.isEmpty()) {
messageDisplay->append(QString("[%1] 错误: 设备SN不能为空")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
return;
}
@ -817,7 +881,7 @@ void MainWindow::sendSearchLightStripCommand() {
// 发送消息
if (mqttClient->publish(topic, jsonString)) {
messageDisplay->append(QString("[%1] 发送搜索灯条命令")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
messageDisplay->append(QString("Topic: %1").arg(topic));
messageDisplay->append(QString("Message: %1").arg(jsonString));
messageDisplay->append("---");
@ -826,7 +890,7 @@ void MainWindow::sendSearchLightStripCommand() {
QMessageBox::information(this, "搜索灯条", "不要做其他指令正在搜索灯条请等待2分钟返回结果...");
} else {
messageDisplay->append(QString("[%1] 发送搜索灯条命令失败")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss")));
messageDisplay->append("---");
// 弹出错误提示框
@ -1111,8 +1175,8 @@ void MainWindow::openLightStripManager()
// 显示成功提示
QString resultMessage = QString("已向设备 %1 发送控制命令,控制 %2 个灯条")
.arg(deviceSn)
.arg(sns.size());
.arg(deviceSn)
.arg(sns.size());
QMessageBox::information(this, "成功", resultMessage);
} else {
qDebug() << "发送灯条控制命令失败";
@ -1192,11 +1256,11 @@ void MainWindow::openLightStripManager()
// 在消息显示区域显示发送的消息
messageDisplay->append(QString("[%1] 发送身份信息绑定到设备 %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(deviceSn));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(deviceSn));
messageDisplay->append(QString("主题: %1").arg(topic));
messageDisplay->append(QString("灯条SN: %1, Label1: %2, Label2: %3, Label3: %4")
.arg(sn).arg(label1).arg(label2).arg(label3));
.arg(sn).arg(label1).arg(label2).arg(label3));
} else {
qDebug() << "发送身份信息绑定命令失败";
QMessageBox::warning(this, "错误", "发送MQTT消息失败请检查网络连接");
@ -1206,7 +1270,7 @@ void MainWindow::openLightStripManager()
// 新增:连接分组点亮信号
connect(lightStripManager, &LightStripManager::groupLightRequested,
this, [this](const QString &label1, const QString &label2, const QString &label3,
int rule1, int rule2, int rule3, const QString &color, int flash, int duration, bool sound, int flashInterval) {
int rule1, int rule2, int rule3, const QString &color, int flash, int duration, bool sound, int flashInterval) {
// 检查MQTT连接状态
if (!mqttClient->isConnected()) {
@ -1273,13 +1337,13 @@ void MainWindow::openLightStripManager()
// 在消息显示区域显示发送的消息
messageDisplay->append(QString("[%1] 发送分组点亮命令到设备 %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(deviceSn));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(deviceSn));
messageDisplay->append(QString("主题: %1").arg(topic));
messageDisplay->append(QString("匹配规则: Label1=%1(rule:%2), Label2=%3(rule:%4), Label3=%5(rule:%6)")
.arg(label1).arg(rule1, 2, 10, QChar('0'))
.arg(label2).arg(rule2, 2, 10, QChar('0'))
.arg(label3).arg(rule3, 2, 10, QChar('0')));
.arg(label1).arg(rule1, 2, 10, QChar('0'))
.arg(label2).arg(rule2, 2, 10, QChar('0'))
.arg(label3).arg(rule3, 2, 10, QChar('0')));
} else {
qDebug() << "发送分组点亮命令失败";
QMessageBox::warning(this, "错误", "发送MQTT消息失败请检查网络连接");
@ -1322,9 +1386,62 @@ void MainWindow::publishMqttMessage(const QString &topic, const QString &message
if (mqttClient && mqttClient->isConnected()) {
mqttClient->publish(topic, message);
messageDisplay->append(QString("[%1] 发送消息到主题: %2")
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(topic));
.arg(QDateTime::currentDateTime().toString("hh:mm:ss"))
.arg(topic));
} else {
QMessageBox::warning(this, "错误", "MQTT未连接无法发送消息");
}
}
void MainWindow::createMenus()
{
// 直接创建菜单,不使用成员变量
QMenu *helpMenu = menuBar()->addMenu("帮助(&H)");
QAction *aboutAction = helpMenu->addAction("关于程序(&A)");
connect(aboutAction, &QAction::triggered, this, &MainWindow::showAbout);
QAction *useGuideAction = helpMenu->addAction("使用说明(&U)");
connect(useGuideAction, &QAction::triggered, this, &MainWindow::showUseGuide);
}
void MainWindow::showAbout()
{
QMessageBox::about(this, "关于程序",
"兔喜MQTT测试程序\n\n"
"版本: 1.2.0\n"
"构建日期: 2025-09-12\n\n"
"功能特性:\n"
"• 修复清空全部sn未生效的问题\n"
"• 修复label匹配值错误的问题\n"
"• 匹配系统颜色。修复浅色模式下字体看不清\n"
"• 增加窗口版本显示\n");
}
void MainWindow::showUseGuide()
{
QMessageBox::about(this, "使用说明",
"切换主题后看不清文字,请重启应用!!\n"
"1. 连接MQTT服务器\n"
"2. 输入需要测试的设备SN\n"
"3. 首次使用先升级版本1.1.16及之前的版本可能不支持升级\n"
"4. 可以做其他指令查询版本、搜索灯带、灯带SN管理\n\n"
);
}
// 新增:检测系统主题并返回适合的文字颜色
QString MainWindow::getTextColorForTheme() const {
// 获取系统调色板
QPalette palette = QApplication::palette();
// 检查窗口背景色的亮度来判断是否为深色主题
QColor backgroundColor = palette.color(QPalette::Window);
// 计算亮度 (使用相对亮度公式)
double luminance = (0.299 * backgroundColor.red() +
0.587 * backgroundColor.green() +
0.114 * backgroundColor.blue()) / 255.0;
// 如果背景较暗亮度小于0.5),使用白色文字;否则使用黑色文字
return (luminance < 0.5) ? "white" : "black";
}

View File

@ -29,6 +29,12 @@
#include <QGuiApplication>
#include "mqttclient.h"
#include "lightstripmanager.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QMessageBox>
#include <QApplication> // 新增:用于获取系统调色板
#include <QPalette>
class MainWindow : public QMainWindow
{
@ -38,8 +44,23 @@ public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void showAbout(); // 简化为关于对话框
void showUseGuide(); // 新增:使用说明对话框
private:
void setupUI();
void createMenus(); // 重命名为更清晰的函数名
// 不使用成员变量存储菜单指针,直接在函数中创建
// void setupMenuBar();
// 注释掉菜单相关成员变量
// QAction *versionUpdateAction;
// 新增:公共接口
QString getDeviceSn() const;
QString getTextColorForTheme() const;
bool isMqttConnected() const;
void publishMqttMessage(const QString &topic, const QString &message);
@ -65,8 +86,6 @@ private slots:
void onLightStripManagerClosed(); // 新增:灯条管理器关闭时的处理
private:
void setupUI();
void setupMenuBar();
void setupToolBar();
void updateConnectionStatus(bool connected);
void processOtaMessage(const QJsonObject &otaData);