调整菜单栏

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);
@ -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() {
@ -1328,3 +1392,56 @@ void MainWindow::publishMqttMessage(const QString &topic, const QString &message
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);