123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #include "MainWnd.h"
- #include "ui_MainWnd.h"
- #include "Login.h"
- #include <QMouseEvent>
- #include <qDebug>
- #include "OriginalWnd/OriginalWnd.h"
- MainWnd::MainWnd(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWnd)
- {
- ui->setupUi(this);
- // connect(ui->stackedWidget_3, &QStackedWidget::currentChanged,this, &MainWnd::onCurrentWidgetChanged);
- initForm();
- }
- MainWnd::~MainWnd()
- {
- delete ui;
- }
- void MainWnd::initForm()
- {
- // 隐藏原生标题栏
- setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
- // 窗口背景透明
- setAttribute(Qt::WA_TranslucentBackground, true);
- QPixmap pixmap(":/images/light/logo1.png");
- QPixmap scaledPixmap = pixmap.scaled(90, 49, Qt::KeepAspectRatio);
- ui->label_logo->setPixmap(scaledPixmap);
- Login *login = new Login;
- ui->stackedWidget_3->addWidget(login);
- ui->stackedWidget_3->setCurrentIndex(ui->stackedWidget_3->indexOf(login));
- //登录监听
- ui->label_logo->installEventFilter(this);
- ui->stackedWidget_3->installEventFilter(this);
- //安装监听事件,点击左侧按钮图标变化
- ui->tabBondBtn->installEventFilter(this);
- ui->tabBondBtn_2->installEventFilter(this);
- ui->tabBondBtn_3->installEventFilter(this);
- ui->tabBondBtn_4->installEventFilter(this);
- ui->tabBondBtn_5->installEventFilter(this);
- ui->tabBondBtn_6->installEventFilter(this);
- ui->tabBondBtn_7->installEventFilter(this);
- ui->tabBondBtn_8->installEventFilter(this);
- ui->tabBondBtn_9->installEventFilter(this);
- }
- bool MainWnd::eventFilter(QObject *obj, QEvent *event)
- {
- if (obj == this->ui->stackedWidget_3)
- {
- onCurrentWidgetChanged();
- }
- if (obj == this->ui->label_logo)
- {
- //判断事件类型是否为鼠标事件
- if (event->type() == QEvent::MouseButtonPress)
- {
- //转换为鼠标事件
- QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
- //判断鼠标左键点击
- if (mouseenevt->button() == Qt::LeftButton)
- {
- Login *login = new Login;
- ui->stackedWidget_3->addWidget(login);
- ui->stackedWidget_3->setCurrentIndex(ui->stackedWidget_3->indexOf(login));
- return true;
- }
- }
- }
- static const QStringList buttonNames = {
- "home",
- "Productionn assistance",
- "Program",
- "Message",
- "Module",
- "Disposition",
- "Diagnosis",
- "System",
- "Help"
- };
- static QToolButton* lastHoveredButton = nullptr; // 用于跟踪上一个悬停的按钮
- static QMap<QToolButton*, QString> buttonLightIcons; // 存储每个按钮的亮色图标路径
- // 处理鼠标按下事件或鼠标悬停进入事件
- if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::HoverEnter) {
- if (obj && obj->inherits("QToolButton")) {
- QToolButton* button = static_cast<QToolButton*>(obj);
- // 恢复所有按钮为亮色图标(如果没有选中)
- for (int i = 0; i < buttonNames.size(); ++i) {
- QToolButton* currentButton = nullptr;
- switch (i) {
- case 0: currentButton = ui->tabBondBtn; break;
- case 1: currentButton = ui->tabBondBtn_2; break;
- case 2: currentButton = ui->tabBondBtn_3; break;
- case 3: currentButton = ui->tabBondBtn_4; break;
- case 4: currentButton = ui->tabBondBtn_5; break;
- case 5: currentButton = ui->tabBondBtn_6; break;
- case 6: currentButton = ui->tabBondBtn_7; break;
- case 7: currentButton = ui->tabBondBtn_8; break;
- case 8: currentButton = ui->tabBondBtn_9; break;
- }
- if (currentButton) {
- QString lightIconPath = QString(":/images/light/%1.png").arg(buttonNames[i]);
- if (currentButton == button) {
- // 当前悬停的按钮,设置为深色图标
- QString deepIconPath = QString(":/images/deep/%1.png").arg(buttonNames[i]);
- currentButton->setIcon(QIcon(deepIconPath));
- currentButton->setChecked(true);
- } else if (!currentButton->isChecked()) {
- // 恢复为亮色图标
- buttonLightIcons[currentButton] = lightIconPath; // 存储亮色图标路径
- currentButton->setIcon(QIcon(lightIconPath));
- }
- }
- }
- // 如果上一个悬停按钮存在且不是当前按钮,恢复其亮色图标
- if (lastHoveredButton && lastHoveredButton != button && !lastHoveredButton->isChecked()) {
- QString lightIconPath = buttonLightIcons.value(lastHoveredButton);
- if (!lightIconPath.isEmpty()) {
- lastHoveredButton->setIcon(QIcon(lightIconPath));
- }
- }
- lastHoveredButton = button; // 更新为当前悬停的按钮
- }
- return true; // 表示事件已处理
- }
- // 处理鼠标离开事件
- if (event->type() == QEvent::HoverLeave) {
- if (obj && obj->inherits("QToolButton")) {
- QToolButton* button = static_cast<QToolButton*>(obj);
- if (button == lastHoveredButton && !button->isChecked()) {
- QString lightIconPath = buttonLightIcons.value(button);
- if (!lightIconPath.isEmpty()) {
- button->setIcon(QIcon(lightIconPath));
- }
- lastHoveredButton = nullptr; // 重置上一个悬停的按钮
- }
- }
- return true; // 表示事件已处理
- }
- return QWidget::eventFilter(obj, event); // 如果没有处理返回默认事件处理
- }
- void MainWnd::onCurrentWidgetChanged() {
- QWidget *currentWidget = ui->stackedWidget_3->currentWidget();
- if (currentWidget) {
- QString currentWidgetName = currentWidget->objectName();
- if (currentWidgetName == "OriginalWnd") {
- ui->tabBondBtn->setIcon(QIcon(":/images/deep/home.png"));
- ui->tabBondBtn->setChecked(true);
- ui->tabBondBtn->setStyleSheet("background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #949FE8,stop:1 #2D309B);color:#FFFFFF;");
- ui->tabBondBtn->update();
- } else {
- ui->tabBondBtn->setStyleSheet(""); // 恢复默认样式
- }
- }
- }
- void MainWnd::on_tabBondBtn_clicked()
- {
- OriginalWnd *originalWnd = new OriginalWnd(this);
- ui->stackedWidget_3->addWidget(originalWnd);
- ui->stackedWidget_3->setCurrentIndex(ui->stackedWidget_3->indexOf(originalWnd));
- }
- void MainWnd::on_pushButton_clicked()
- {
- qApp->quit();
- }
|