From 551f24da50829505d8c8e237d0596e8cb61d346c Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Fri, 3 Jan 2025 15:10:55 +0800 Subject: [PATCH] tmpcode --- doc/main.jsonc | 90 ++++++++++++++++++++++ mainwindow.cpp | 188 +++++++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 30 ++++++++ mainwindow.ui | 150 +++++++++++++++++++----------------- seismic_monitor.ui | 36 +++++++++ 5 files changed, 422 insertions(+), 72 deletions(-) create mode 100644 doc/main.jsonc diff --git a/doc/main.jsonc b/doc/main.jsonc new file mode 100644 index 0000000..8fd74c9 --- /dev/null +++ b/doc/main.jsonc @@ -0,0 +1,90 @@ +[{ + "slot":3, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":4, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":5, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":6, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":7, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":8, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":9, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":10, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":11, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":12, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":13, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":14, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":15, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":16, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}, +{ + "slot":17, + "slot_type":0, + "chan_type":"acceleration", + "rack_type":"Single" +}] \ No newline at end of file diff --git a/mainwindow.cpp b/mainwindow.cpp index 41a26bd..2aafe01 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,15 +1,203 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + + btnGroup_slot = new QButtonGroup(this); + btnGroup_slot->addButton(ui->pushButton_slot3); + btnGroup_slot->addButton(ui->pushButton_slot4); + btnGroup_slot->addButton(ui->pushButton_slot5); + btnGroup_slot->addButton(ui->pushButton_slot6); + btnGroup_slot->addButton(ui->pushButton_slot7); + btnGroup_slot->addButton(ui->pushButton_slot8); + btnGroup_slot->addButton(ui->pushButton_slot9); + btnGroup_slot->addButton(ui->pushButton_slot10); + btnGroup_slot->addButton(ui->pushButton_slot11); + btnGroup_slot->addButton(ui->pushButton_slot12); + btnGroup_slot->addButton(ui->pushButton_slot13); + btnGroup_slot->addButton(ui->pushButton_slot14); + btnGroup_slot->addButton(ui->pushButton_slot15); + btnGroup_slot->addButton(ui->pushButton_slot16); + btnGroup_slot->addButton(ui->pushButton_slot17); + + createMenu(); } MainWindow::~MainWindow() { delete ui; } +void MainWindow::createMenu() +{ + QList buttonList = btnGroup_slot->buttons(); + for (int i = 0; i < buttonList.count(); i++){ + slot_type.insert(buttonList[i]->objectName(),""); + } + for (int i = 0; i < buttonList.count(); i++){ + if(slot_type[buttonList[i]->objectName()] == "") + createMenu(QString("%1").arg(i + 1), (QPushButton*)buttonList[i]); + else + createMenuSet(QString("%1").arg(i + 1), (QPushButton*)buttonList[i]); + } +} +void MainWindow::createMenu(const QString& rootTitle, QPushButton* parent ) +{ + // 创建主菜单 + qDebug() << "createMenu" << parent->objectName() << endl; + QMenu *mainMenu = new QMenu(rootTitle, parent); + + // 创建第一层子菜单:/30 振电器模块 + QMenu *monitors = new QMenu("监视器", mainMenu); + QMenu *relays = new QMenu("/30 继电器模块", mainMenu); + QMenu *keyphasor = new QMenu("/25 键相模块", mainMenu); + + // 创建第二层子菜单:/40 振动板卡 + QMenu *proximitor_menu = new QMenu("/40 振动板卡", monitors); + QMenu *rpm_menu = new QMenu("/50 转速板卡", monitors); + + // 创建第三层子菜单:/40 单板卡、三冗余板卡 + QAction *proximitor_1 = proximitor_menu->addAction("/40 单板卡"); + QAction *proximitor_2 = proximitor_menu->addAction("/40 三冗余板卡"); + + QAction *rpm_1 = rpm_menu->addAction("/50 单板卡"); + + + // 创建第二层子菜单:/25 键相模块 + QAction *keyphasor_1 = keyphasor->addAction("/25 单板卡"); + QAction *keyphasor_2 = keyphasor->addAction("/25 两板卡"); + + // 创建第二层子菜单:/30 继电器模块 + QAction *relays_1 = relays->addAction("/30 单板卡"); + QAction *relays_2 = relays->addAction("/30 三冗余板卡"); + + // 将子菜单加入上一级菜单 + monitors->addMenu(proximitor_menu); // 将第二层加入第一层 + monitors->addMenu(rpm_menu); // 第二层另一个子菜单 + + mainMenu->addMenu(monitors); // 将第一层加入主菜单 + mainMenu->addMenu(relays); + mainMenu->addMenu(keyphasor); + QAction *reset = mainMenu->addAction("重置模块"); + parent->setContextMenuPolicy(Qt::CustomContextMenu); + connect(parent,&QPushButton::customContextMenuRequested,[=](const QPoint &pos) + { + qDebug() << pos << endl; + mainMenu->exec(QCursor::pos()); + }); + // 连接所有菜单项 + QObject::connect(proximitor_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(proximitor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(rpm_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(relays_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(relays_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(keyphasor_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(keyphasor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); + QObject::connect(reset, &QAction::triggered,this, &MainWindow::onMenuActionTriggered); +} +void MainWindow::createMenuSet(const QString& rootTitle, QPushButton* parent ) +{ + // 创建主菜单 + qDebug() << "createMenu" << parent->objectName() << endl; + QMenu *mainMenu = new QMenu(rootTitle, parent); + + QAction *option = mainMenu->addAction("通道配置…"); + QAction *set_points = mainMenu->addAction("触发配置…"); + QAction *point_names = mainMenu->addAction("测点名称"); +} + +// 清除菜单中所有动作的属性 +void MainWindow::clearMenuProperties(QMenu* menu) +{ + // 遍历菜单的所有动作 + if (!menu) { + qWarning() << "菜单为空,无法清除属性!"; + return; + } + + // 遍历菜单的所有动作 + for (QAction* action : menu->actions()) { + if (!action) continue; + + // 检查是否有子菜单 + if (QMenu* subMenu = action->menu()) { + clearMenuProperties(subMenu); // 递归处理子菜单 + } + + // 清除动作的属性 + action->setProperty("customProperty", QVariant()); + qDebug() << "清除了属性,动作:" << action->text(); + } +} + +void MainWindow::onMenuActionTriggered() +{ + qDebug() << "onMenuActionTriggered()" << endl; + QAction *action = qobject_cast(sender()); + if (action) { + // 获取触发动作的父菜单 + QMenu *menu = qobject_cast(action->parent()); + // 遍历所有父菜单,直到找到按钮 + while (menu) { + QPushButton *button = qobject_cast(menu->parent()); + if (button) { + qDebug() << "子菜单项被点击,所属按钮:" << button->objectName(); + button->setText(action->text()); + slot_type[button->objectName()] = action->text(); + QMenu* menu = button->menu(); + if (!menu) { + qWarning() << "菜单为空,无法清除属性!"; + return; + } + clearMenuProperties(button->menu()); + createMenu(); + break; // 找到按钮后,跳出循环 + } + // 如果没有找到按钮,继续向上查找 + menu = qobject_cast(menu->parent()); + } + } +} + +void MainWindow::OnButtonGroup(QAbstractButton * slot_btn, bool checked) +{ + if(slot_btn != NULL) + { + qDebug() << slot_btn->objectName(); + qDebug() << checked; + QString str; + QList buttonList = btnGroup_slot->buttons(); + for (int i = 0; i < buttonList.count(); i++){ + + } + } +} + +void MainWindow::on_pushButton_slot_clicked() +{ + +} + + +void MainWindow::on_pushButton_chan_clicked() +{ + +} + + +void MainWindow::on_pushButton_alarm_clicked() +{ + +} + + +void MainWindow::on_pushButton_point_name_clicked() +{ + +} diff --git a/mainwindow.h b/mainwindow.h index 4643e32..c7d9a70 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,9 @@ #define MAINWINDOW_H #include +#include +#include +#include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -17,5 +20,32 @@ public: private: Ui::MainWindow *ui; + + QAction * action_menu_monitors; + QAction * action_menu_keyphasor; + QAction * action_menu_relays; + QAction * action_menu_reset; + + //声明菜单 + QMenu * button_menu; + + QMap slot_type; + + QButtonGroup * btnGroup_slot = nullptr; + + void createMenu(); + void createMenu(const QString& rootTitle, QPushButton* button = nullptr); + void createMenuSet(const QString& rootTitle, QPushButton* button = nullptr); + void clearMenuProperties(QMenu* menu); + +private slots: + + void OnButtonGroup(QAbstractButton *, bool); + + void onMenuActionTriggered(); + void on_pushButton_slot_clicked(); + void on_pushButton_chan_clicked(); + void on_pushButton_alarm_clicked(); + void on_pushButton_point_name_clicked(); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 2830a26..adf4f6c 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -259,7 +259,7 @@ 9 - + 0 @@ -272,7 +272,7 @@ - + 0 @@ -285,7 +285,7 @@ - + 0 @@ -298,7 +298,7 @@ - + 0 @@ -368,7 +368,7 @@ - 电源 + 1 Qt::AlignCenter @@ -390,7 +390,7 @@ - 1 + 2 Qt::AlignCenter @@ -412,7 +412,7 @@ - 2 + 3 Qt::AlignCenter @@ -434,7 +434,7 @@ - 3 + 4 Qt::AlignCenter @@ -456,7 +456,7 @@ - 4 + 5 Qt::AlignCenter @@ -478,7 +478,7 @@ - 5 + 6 Qt::AlignCenter @@ -500,7 +500,7 @@ - 6 + 7 Qt::AlignCenter @@ -522,7 +522,7 @@ - 7 + 8 Qt::AlignCenter @@ -544,7 +544,7 @@ - 8 + 9 Qt::AlignCenter @@ -566,7 +566,7 @@ - 9 + 10 Qt::AlignCenter @@ -588,7 +588,7 @@ - 10 + 11 Qt::AlignCenter @@ -610,7 +610,7 @@ - 11 + 12 Qt::AlignCenter @@ -632,7 +632,7 @@ - 12 + 13 Qt::AlignCenter @@ -654,7 +654,7 @@ - 13 + 14 Qt::AlignCenter @@ -676,7 +676,7 @@ - 14 + 15 Qt::AlignCenter @@ -698,7 +698,7 @@ - 15 + 16 Qt::AlignCenter @@ -720,7 +720,7 @@ - 16 + 17 Qt::AlignCenter @@ -790,7 +790,51 @@ - + + + + 50 + 280 + + + + + 16777215 + 280 + + + + Qt::DefaultContextMenu + + + + + + + + + + + 50 + 280 + + + + + 16777215 + 280 + + + + Qt::DefaultContextMenu + + + + + + + + 50 @@ -809,7 +853,7 @@ - + 50 @@ -828,7 +872,7 @@ - + 50 @@ -847,7 +891,7 @@ - + 50 @@ -866,7 +910,7 @@ - + 50 @@ -885,7 +929,7 @@ - + 50 @@ -904,7 +948,7 @@ - + 50 @@ -923,7 +967,7 @@ - + 50 @@ -942,7 +986,7 @@ - + 50 @@ -961,7 +1005,7 @@ - + 50 @@ -980,7 +1024,7 @@ - + 50 @@ -999,7 +1043,7 @@ - + 50 @@ -1018,45 +1062,7 @@ - - - - 50 - 280 - - - - - 16777215 - 280 - - - - - - - - - - - - 50 - 280 - - - - - 16777215 - 280 - - - - - - - - - + 50 diff --git a/seismic_monitor.ui b/seismic_monitor.ui index 9f4820c..6e0534b 100644 --- a/seismic_monitor.ui +++ b/seismic_monitor.ui @@ -1219,6 +1219,42 @@ + + + + 340 + 210 + 45 + 62 + + + + + + + ===> + + + + + + + = + + + Qt::AlignCenter + + + + + + + <=== + + + + +