MainWnd.cpp 6.7 KB

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