MainWnd.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "MainWnd.h"
  2. #include "ui_MainWnd.h"
  3. #include "Login.h"
  4. #include <QMouseEvent>
  5. #include <qDebug>
  6. #include "OriginalWnd/OriginalWnd.h"
  7. MainWnd::MainWnd(QWidget *parent)
  8. : QMainWindow(parent)
  9. , ui(new Ui::MainWnd)
  10. {
  11. ui->setupUi(this);
  12. initForm();
  13. }
  14. MainWnd::~MainWnd()
  15. {
  16. delete ui;
  17. }
  18. void MainWnd::initForm()
  19. {
  20. // 隐藏原生标题栏
  21. setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
  22. // 窗口背景透明
  23. setAttribute(Qt::WA_TranslucentBackground, true);
  24. QPixmap pixmap(":/images/light/logo1.png");
  25. QPixmap scaledPixmap = pixmap.scaled(90, 49, Qt::KeepAspectRatio);
  26. ui->label_logo->setPixmap(scaledPixmap);
  27. Login *login = new Login;
  28. ui->stackedWidget_3->addWidget(login);
  29. ui->stackedWidget_3->setCurrentIndex(ui->stackedWidget_3->indexOf(login));
  30. //登录监听
  31. ui->label_logo->installEventFilter(this);
  32. //安装监听事件,点击左侧按钮图标变化
  33. ui->tabBondBtn->installEventFilter(this);
  34. ui->tabBondBtn_2->installEventFilter(this);
  35. ui->tabBondBtn_3->installEventFilter(this);
  36. ui->tabBondBtn_4->installEventFilter(this);
  37. ui->tabBondBtn_5->installEventFilter(this);
  38. ui->tabBondBtn_6->installEventFilter(this);
  39. ui->tabBondBtn_7->installEventFilter(this);
  40. ui->tabBondBtn_8->installEventFilter(this);
  41. ui->tabBondBtn_9->installEventFilter(this);
  42. }
  43. bool MainWnd::eventFilter(QObject *obj, QEvent *event)
  44. {
  45. if (obj == this->ui->label_logo)
  46. {
  47. //判断事件类型是否为鼠标事件
  48. if (event->type() == QEvent::MouseButtonPress)
  49. {
  50. //转换为鼠标事件
  51. QMouseEvent* mouseenevt = static_cast<QMouseEvent*>(event);
  52. //判断鼠标左键点击
  53. if (mouseenevt->button() == Qt::LeftButton)
  54. {
  55. Login *login = new Login;
  56. ui->stackedWidget_3->addWidget(login);
  57. ui->stackedWidget_3->setCurrentIndex(ui->stackedWidget_3->indexOf(login));
  58. return true;
  59. }
  60. }
  61. }
  62. static const QStringList buttonNames = {
  63. "home",
  64. "Productionn assistance",
  65. "Program",
  66. "Message",
  67. "Module",
  68. "Disposition",
  69. "Diagnosis",
  70. "System",
  71. "Help"
  72. };
  73. static QToolButton* lastHoveredButton = nullptr; // 用于跟踪上一个悬停的按钮
  74. static QMap<QToolButton*, QString> buttonLightIcons; // 存储每个按钮的亮色图标路径
  75. // 处理鼠标按下事件或鼠标悬停进入事件
  76. if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::HoverEnter) {
  77. if (obj && obj->inherits("QToolButton")) {
  78. QToolButton* button = static_cast<QToolButton*>(obj);
  79. // 恢复所有按钮为亮色图标(如果没有选中)
  80. for (int i = 0; i < buttonNames.size(); ++i) {
  81. QToolButton* currentButton = nullptr;
  82. switch (i) {
  83. case 0: currentButton = ui->tabBondBtn; break;
  84. case 1: currentButton = ui->tabBondBtn_2; break;
  85. case 2: currentButton = ui->tabBondBtn_3; break;
  86. case 3: currentButton = ui->tabBondBtn_4; break;
  87. case 4: currentButton = ui->tabBondBtn_5; break;
  88. case 5: currentButton = ui->tabBondBtn_6; break;
  89. case 6: currentButton = ui->tabBondBtn_7; break;
  90. case 7: currentButton = ui->tabBondBtn_8; break;
  91. case 8: currentButton = ui->tabBondBtn_9; break;
  92. }
  93. if (currentButton) {
  94. QString lightIconPath = QString(":/images/light/%1.png").arg(buttonNames[i]);
  95. if (currentButton == button) {
  96. // 当前悬停的按钮,设置为深色图标
  97. QString deepIconPath = QString(":/images/deep/%1.png").arg(buttonNames[i]);
  98. currentButton->setIcon(QIcon(deepIconPath));
  99. currentButton->setChecked(true);
  100. } else if (!currentButton->isChecked()) {
  101. // 恢复为亮色图标
  102. buttonLightIcons[currentButton] = lightIconPath; // 存储亮色图标路径
  103. currentButton->setIcon(QIcon(lightIconPath));
  104. }
  105. }
  106. }
  107. // 如果上一个悬停按钮存在且不是当前按钮,恢复其亮色图标
  108. if (lastHoveredButton && lastHoveredButton != button && !lastHoveredButton->isChecked()) {
  109. QString lightIconPath = buttonLightIcons.value(lastHoveredButton);
  110. if (!lightIconPath.isEmpty()) {
  111. lastHoveredButton->setIcon(QIcon(lightIconPath));
  112. }
  113. }
  114. lastHoveredButton = button; // 更新为当前悬停的按钮
  115. }
  116. return true; // 表示事件已处理
  117. }
  118. // 处理鼠标离开事件
  119. if (event->type() == QEvent::HoverLeave) {
  120. if (obj && obj->inherits("QToolButton")) {
  121. QToolButton* button = static_cast<QToolButton*>(obj);
  122. if (button == lastHoveredButton && !button->isChecked()) {
  123. QString lightIconPath = buttonLightIcons.value(button);
  124. if (!lightIconPath.isEmpty()) {
  125. button->setIcon(QIcon(lightIconPath));
  126. }
  127. lastHoveredButton = nullptr; // 重置上一个悬停的按钮
  128. }
  129. }
  130. return true; // 表示事件已处理
  131. }
  132. return QWidget::eventFilter(obj, event); // 如果没有处理返回默认事件处理
  133. }
  134. // void MainWnd::on_pushButton_4_clicked()
  135. // {
  136. // Demo001_1 *demo001_1 = new Demo001_1;
  137. // ui->switchShowPageUI->addWidget(demo001_1);
  138. // ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_1));
  139. // }
  140. // void MainWnd::on_pushButton_5_clicked()
  141. // {
  142. // Demo001_2 *demo001_2 = new Demo001_2;
  143. // ui->switchShowPageUI->addWidget(demo001_2);
  144. // ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_2));
  145. // }
  146. // void MainWnd::on_pushButton_6_clicked()
  147. // {
  148. // Demo001_3 *demo001_3 = new Demo001_3;
  149. // ui->switchShowPageUI->addWidget(demo001_3);
  150. // ui->switchShowPageUI->setCurrentIndex(ui->switchShowPageUI->indexOf(demo001_3));
  151. // }
  152. void MainWnd::on_tabBondBtn_clicked()
  153. {
  154. OriginalWnd *originalWnd = new OriginalWnd;
  155. ui->stackedWidget_3->addWidget(originalWnd);
  156. ui->stackedWidget_3->setCurrentIndex(ui->stackedWidget_3->indexOf(originalWnd));
  157. }