From ae5701d2411fee6902ae0ba27c8f28198bab1ebc Mon Sep 17 00:00:00 2001 From: xiaopaigui <2568611341@qq.com> Date: Thu, 12 Dec 2019 17:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A8=8B=E5=BA=8F=E6=89=98?= =?UTF-8?q?=E7=9B=98=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dialog_set.cpp | 1 - mainwindow.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++-- mainwindow.h | 19 ++++++++++++- mainwindow.ui | 4 +-- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/dialog_set.cpp b/dialog_set.cpp index 1899620..8374810 100644 --- a/dialog_set.cpp +++ b/dialog_set.cpp @@ -52,5 +52,4 @@ void Dialog_set::on_pushButton_add_key_clicked() set_cmd_list.append(wifi_cmd); } - } diff --git a/mainwindow.cpp b/mainwindow.cpp index ec290d3..41e2b61 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -36,6 +36,8 @@ MainWindow::MainWindow(QWidget *parent) : ui->tableWidget->setColumnWidth(0, ui->tableWidget->width()); + + mSysTrayIcon = NULL; } MainWindow::~MainWindow() @@ -118,8 +120,6 @@ void MainWindow::do_msg(QByteArray msg) ui->tableWidget->insertRow(ui->tableWidget->rowCount()); ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0, new QTableWidgetItem(todo_list.at(i))); } - - } } @@ -313,5 +313,74 @@ void MainWindow::load_set(void) } +void MainWindow::closeEvent(QCloseEvent *event) +{ + event->ignore(); + this->hide(); + if(mSysTrayIcon == NULL){ + + + mSysTrayIcon = new QSystemTrayIcon(this);//新建QSystemTrayIcon对象 + QIcon icon = QIcon(":/log_l.ico");//新建托盘要显示的icon + mSysTrayIcon->setIcon(icon);//将icon设到QSystemTrayIcon对象中 + mSysTrayIcon->setToolTip(QString::fromLocal8Bit("魔镜,魔镜..."));//当鼠标移动到托盘上的图标时,会显示此处设置的内容 + + connect(mSysTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));//给QSystemTrayIcon添加槽函数 + + + createActions();//建立托盘操作的菜单 + createMenu(); + mSysTrayIcon->show();//在系统托盘显示此对象 + + + } + +} + +void MainWindow::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason) +{ + switch(reason){ + case QSystemTrayIcon::Trigger://单击鼠标左键 + break; + case QSystemTrayIcon::DoubleClick: + this->show(); + break; + default: + break; + } +} +void MainWindow::createActions() +{ + mShowMainAction = new QAction(QString::fromLocal8Bit("显示主界面"),this); + connect(mShowMainAction,SIGNAL(triggered()),this,SLOT(on_showMainAction())); + + mExitAppAction = new QAction(QString::fromLocal8Bit("退出"),this); + connect(mExitAppAction,SIGNAL(triggered()),this,SLOT(on_exitAppAction())); + +} +void MainWindow::createMenu() +{ + mMenu = new QMenu(this); + mMenu->addAction(mShowMainAction); + + mMenu->addSeparator(); + + mMenu->addAction(mExitAppAction); + + mSysTrayIcon->setContextMenu(mMenu); +} + +void MainWindow::on_showMainAction() +{ + this->show(); + mSysTrayIcon->hide(); +} + +void MainWindow::on_exitAppAction() +{ + delete mSysTrayIcon; + exit(0); +} diff --git a/mainwindow.h b/mainwindow.h index 83eaa65..b9c2667 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,7 @@ #include #include #include +#include @@ -13,7 +14,7 @@ #include "dialog_about.h" #include - +#include namespace Ui { class MainWindow; @@ -39,6 +40,15 @@ public: void load_set(void); QString ini_fix_id; + + QSystemTrayIcon *mSysTrayIcon; + QMenu *mMenu; + QAction *mShowMainAction; + QAction *mExitAppAction; + + void createActions(); + void createMenu(); + private slots: void connect_flush(void); @@ -54,6 +64,9 @@ private slots: void on_tableWidget_customContextMenuRequested(const QPoint &pos); + void on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason); + void on_showMainAction(); + void on_exitAppAction(); private: Ui::MainWindow *ui; @@ -62,6 +75,10 @@ private: void mirror_uartwrite_set(); void flush_mirror_faaddr(); + +protected: + void closeEvent(QCloseEvent *event);//重写窗口关闭事件 + }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 741cb23..bd66b88 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -17,9 +17,9 @@ - 0 + -10 0 - 301 + 311 351 -- Gitee