Qt-QSystemTrayIcon实现系统托盘
相关资料:
https://blog.csdn.net/u011417605/article/details/51322997
.pro
1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 CONFIG += c++11 6 7 # The following define makes your compiler emit warnings if you use 8 # any Qt feature that has been marked deprecated (the exact warnings 9 # depend on your compiler). Please consult the documentation of the 10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile if it uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \ 20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 LIBS += -lUser32 // 这个window的引用 29 30 31 # Default rules for deployment. 32 qnx: target.path = /tmp/$${TARGET}/bin 33 else: unix:!android: target.path = /opt/$${TARGET}/bin 34 !isEmpty(target.path): INSTALLS += target 35 36 RESOURCES += \ 37 QtSystemTrayIcon.qrc
main.cpp
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <Windows.h> 5 6 #include <QMainWindow> 7 #include <QSystemTrayIcon> 8 #include <QMenu> 9 #include <QAction> 10 #include <QDebug> 11 12 QT_BEGIN_NAMESPACE 13 namespace Ui { class MainWindow; } 14 QT_END_NAMESPACE 15 16 class MainWindow : public QMainWindow 17 { 18 Q_OBJECT 19 20 public: 21 MainWindow(QWidget *parent = nullptr); 22 ~MainWindow(); 23 24 virtual void changeEvent(QEvent *event); 25 private: 26 void activeTray(QSystemTrayIcon::ActivationReason reason); 27 28 void showWindow();//显示窗体 29 void showMessage();//消息框 30 void showMenu();//显示菜单 31 private: 32 Ui::MainWindow *ui; 33 34 QSystemTrayIcon *m_systemTray; 35 QMenu *m_menu; 36 QAction *m_action1; 37 QAction *m_action2; 38 }; 39 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 // 菜单的建立 10 m_menu = new QMenu(this); 11 m_action1 = new QAction(m_menu); 12 m_action2 = new QAction(m_menu); 13 m_action1->setText("Show Window"); 14 m_action2->setText("Show Message"); 15 m_menu->addAction(m_action1); 16 m_menu->addAction(m_action2); 17 connect(m_action1, &QAction::triggered, this, &MainWindow::showWindow); 18 connect(m_action2, &QAction::triggered, this, &MainWindow::showMessage); 19 // 托盘的建立 20 m_systemTray = new QSystemTrayIcon(this); 21 m_systemTray->setContextMenu(m_menu); 22 m_systemTray->setIcon(QIcon(":/new/prefix1/ico.png")); 23 m_systemTray->setToolTip("SystemTray Program"); 24 m_systemTray->show(); 25 connect(m_systemTray, &QSystemTrayIcon::activated, this, &MainWindow::activeTray);//点击托盘,执行相应的动作 26 connect(m_systemTray, &QSystemTrayIcon::messageClicked, this, &MainWindow::showWindow);//点击消息框,显示主窗口 27 } 28 29 MainWindow::~MainWindow() 30 { 31 delete ui; 32 } 33 34 void MainWindow::changeEvent(QEvent *event) 35 { 36 if(event->type() == QEvent::WindowStateChange) 37 { 38 if(windowState() & Qt::WindowMinimized) 39 { 40 hide(); 41 } 42 } 43 QWidget::changeEvent(event); 44 } 45 46 void MainWindow::activeTray(QSystemTrayIcon::ActivationReason reason) 47 { 48 qDebug() << "activeTray:" << reason; 49 switch (reason) 50 { 51 case QSystemTrayIcon::Context: 52 showMenu(); 53 break; 54 // case QSystemTrayIcon::DoubleClick: 55 // showMessage(); 56 // break; 57 case QSystemTrayIcon::Trigger: 58 showWindow(); 59 break; 60 } 61 } 62 63 void MainWindow::showWindow() 64 { 65 Qt::WindowStates wins; 66 if (windowState() & Qt::WindowMaximized) 67 { 68 wins = Qt::WindowMaximized; 69 } 70 setWindowState(Qt::WindowActive | wins); 71 this->show(); 72 } 73 74 void MainWindow::showMessage() 75 { 76 m_systemTray->showMessage("Information",//消息窗口标题 77 "There is a new message!",//消息内容 78 QSystemTrayIcon::MessageIcon::Information,//消息窗口图标 79 5000);//消息窗口显示时长 80 } 81 82 void MainWindow::showMenu() 83 { 84 m_menu->show(); 85 }
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>MainWindow</class> 4 <widget class="QMainWindow" name="MainWindow"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>458</width> 10 <height>132</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralwidget"/> 17 <widget class="QMenuBar" name="menubar"> 18 <property name="geometry"> 19 <rect> 20 <x>0</x> 21 <y>0</y> 22 <width>458</width> 23 <height>23</height> 24 </rect> 25 </property> 26 </widget> 27 <widget class="QStatusBar" name="statusbar"/> 28 </widget> 29 <resources/> 30 <connections/> 31 </ui>
resources 不再上传
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我