commit 21da40064f4e7931e1b74adc94ff8c0338fcb683
Author: panyufeng <3043130461@qq.com>
Date: Mon Jun 28 00:13:37 2021 +0800
Qt提交自定义图标托盘Demo
diff --git a/Shell_NotifyIcon/Shell_NotifyIcon.pro b/Shell_NotifyIcon/Shell_NotifyIcon.pro
new file mode 100644
index 0000000..158725c
--- /dev/null
+++ b/Shell_NotifyIcon/Shell_NotifyIcon.pro
@@ -0,0 +1,46 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2021-06-26T19:35:05
+#
+#-------------------------------------------------
+
+QT += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+TARGET = Shell_NotifyIcon
+TEMPLATE = app
+
+# The following define makes your compiler emit warnings if you use
+# any feature of Qt which has been marked as deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if you use deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
+
+CONFIG += c++11
+
+SOURCES += \
+ main.cpp \
+ widget.cpp \
+ menudialog.cpp
+
+HEADERS += \
+ widget.h \
+ menudialog.h
+
+FORMS += \
+ widget.ui \
+ menudialog.ui
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+RESOURCES += \
+ resources.qrc
diff --git a/Shell_NotifyIcon/Shell_NotifyIcon.pro.user b/Shell_NotifyIcon/Shell_NotifyIcon.pro.user
new file mode 100644
index 0000000..f9bcabd
--- /dev/null
+++ b/Shell_NotifyIcon/Shell_NotifyIcon.pro.user
@@ -0,0 +1,322 @@
+
+
+
+
+
+ EnvironmentId
+ {09387395-de2a-4726-96ce-a9bc014372d1}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ UTF-8
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ true
+ false
+ 0
+ true
+ true
+ 0
+ 8
+ true
+ 1
+ true
+ true
+ true
+ false
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+ -fno-delayed-template-parsing
+
+ true
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ 桌面
+ 桌面
+ {3b8920a0-93b1-4fd1-9a35-a094ba07b233}
+ 0
+ 0
+ 0
+
+ D:/c++/Shell_NotifyIcon/build-Shell_NotifyIcon-unknown-Debug
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ true
+
+ false
+ false
+ false
+
+
+ true
+ Make
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Build
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Make
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Clean
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Debug
+ Debug
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 2
+ true
+
+
+ D:/c++/Shell_NotifyIcon/build-Shell_NotifyIcon-unknown-Release
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ false
+
+ false
+ false
+ true
+
+
+ true
+ Make
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Build
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Make
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Clean
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Release
+ Release
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 0
+ true
+
+
+ D:/c++/Shell_NotifyIcon/build-Shell_NotifyIcon-unknown-Profile
+
+
+ true
+ qmake
+
+ QtProjectManager.QMakeBuildStep
+ true
+
+ false
+ true
+ true
+
+
+ true
+ Make
+
+ Qt4ProjectManager.MakeStep
+
+ false
+
+
+
+ 2
+ Build
+
+ ProjectExplorer.BuildSteps.Build
+
+
+
+ true
+ Make
+
+ Qt4ProjectManager.MakeStep
+
+ true
+ clean
+
+
+ 1
+ Clean
+
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Profile
+ Profile
+ Qt4ProjectManager.Qt4BuildConfiguration
+ 0
+ true
+
+ 3
+
+
+ 0
+ 部署
+
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+ Deploy Configuration
+
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+ false
+ false
+ 1000
+
+ true
+
+ false
+ false
+ false
+ false
+ true
+ 0.01
+ 10
+ true
+ 1
+ 25
+
+ 1
+ true
+ false
+ true
+ valgrind
+
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+
+ 2
+
+ Shell_NotifyIcon
+
+ Qt4ProjectManager.Qt4RunConfiguration:D:/c++/Shell_NotifyIcon/Shell_NotifyIcon/Shell_NotifyIcon.pro
+ true
+
+ Shell_NotifyIcon.pro
+
+ D:/c++/Shell_NotifyIcon/build-Shell_NotifyIcon-unknown-Debug
+ 3768
+ false
+ true
+ false
+ false
+ true
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 18
+
+
+ Version
+ 18
+
+
diff --git a/Shell_NotifyIcon/app.ico b/Shell_NotifyIcon/app.ico
new file mode 100644
index 0000000..13824e3
Binary files /dev/null and b/Shell_NotifyIcon/app.ico differ
diff --git a/Shell_NotifyIcon/main.cpp b/Shell_NotifyIcon/main.cpp
new file mode 100644
index 0000000..a3c7208
--- /dev/null
+++ b/Shell_NotifyIcon/main.cpp
@@ -0,0 +1,10 @@
+#include "widget.h"
+#include
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ Widget w;
+
+ return a.exec();
+}
diff --git a/Shell_NotifyIcon/menudialog.cpp b/Shell_NotifyIcon/menudialog.cpp
new file mode 100644
index 0000000..f7699fb
--- /dev/null
+++ b/Shell_NotifyIcon/menudialog.cpp
@@ -0,0 +1,51 @@
+#include "menudialog.h"
+#include "ui_menudialog.h"
+
+MenuDialog::MenuDialog(QWidget *parent) :
+ QDialog(parent),
+ ui(new Ui::MenuDialog)
+{
+ ui->setupUi(this);
+
+ initUI();
+
+}
+
+MenuDialog::~MenuDialog()
+{
+ delete ui;
+}
+
+void MenuDialog::initUI()
+{
+ uodateNumbers();
+ updateImage();
+}
+
+
+
+void MenuDialog::uodateNumbers()
+{
+ QString randomString = getRandomString(40);
+ ui->label_number1->setText(randomString.mid(0,8));
+ ui->label_number2->setText(randomString.mid(8,8));
+ ui->label_number3->setText(randomString.mid(16,8));
+ ui->label_number4->setText(randomString.mid(24,8));
+ ui->label_number5->setText(randomString.mid(32,8));
+}
+
+void MenuDialog::updateImage()
+{
+ QImage image(":/resources/gilr.jpg");
+ image = image.scaled(200, 200);
+ ui->label_Image->setPixmap(QPixmap::fromImage(image));
+}
+
+
+void MenuDialog::on_pushButton_exit_clicked()
+{
+ QCoreApplication *app = (static_cast(QCoreApplication::instance()));
+ app->exit(0);
+}
+
+
diff --git a/Shell_NotifyIcon/menudialog.h b/Shell_NotifyIcon/menudialog.h
new file mode 100644
index 0000000..630bca3
--- /dev/null
+++ b/Shell_NotifyIcon/menudialog.h
@@ -0,0 +1,65 @@
+#ifndef MENUDIALOG_H
+#define MENUDIALOG_H
+
+#include
+#include
+#include
+
+
+
+static QString getRandomString(int length) //获取指定长度的字符串
+{
+ qsrand(QDateTime::currentMSecsSinceEpoch());//为随机值设定一个seed
+
+ const char chrs[] = "0123456789";
+ int chrs_size = sizeof(chrs);
+
+ char* ch = new char[length + 1];
+ memset(ch, 0, length + 1);
+ int randomx = 0;
+ for (int i = 0; i < length; ++i)
+ {
+ randomx= rand() % (chrs_size - 1);
+ ch[i] = chrs[randomx];
+ if(i % 8 == 0 && ch[i] == '0'){ //数字开头不能为0
+ i = i-1;
+ }
+ }
+ QString ret(ch);
+ delete[] ch;
+ return ret;
+}
+
+namespace Ui {
+class MenuDialog;
+}
+
+class MenuDialog : public QDialog
+{
+ Q_OBJECT
+
+public:
+ explicit MenuDialog(QWidget *parent = nullptr);
+ ~MenuDialog();
+
+ void uodateNumbers();
+ void updateImage();
+
+private slots:
+ void on_pushButton_exit_clicked();
+
+private:
+ void initUI();
+ Ui::MenuDialog *ui;
+
+protected:
+ virtual void
+ mousePressEvent(QMouseEvent *event){}; //不执行默认的事件,不然一点窗口就消失了
+ virtual void
+ mouseDoubleClickEvent(QMouseEvent *event){};
+ virtual void
+ mouseReleaseEvent(QMouseEvent *event){};
+
+};
+
+#endif // MENUDIALOG_H
diff --git a/Shell_NotifyIcon/menudialog.ui b/Shell_NotifyIcon/menudialog.ui
new file mode 100644
index 0000000..96b2eaa
--- /dev/null
+++ b/Shell_NotifyIcon/menudialog.ui
@@ -0,0 +1,130 @@
+
+
+ MenuDialog
+
+
+
+ 0
+ 0
+ 276
+ 258
+
+
+
+ false
+
+
+ Dialog
+
+
+ -
+
+
-
+
+
+
+ 200
+ 200
+
+
+
+
+ 200
+ 200
+
+
+
+
+
+
+
+ -
+
+
-
+
+
+
-
+
+
+ 数字1
+
+
+
+
+
+
+ -
+
+
+
-
+
+
+ 数字2
+
+
+
+
+
+
+ -
+
+
+
-
+
+
+ 数字3
+
+
+
+
+
+
+ -
+
+
+
-
+
+
+ 数字4
+
+
+
+
+
+
+ -
+
+
+
-
+
+
+ 数字5
+
+
+
+
+
+
+
+
+
+
+ -
+
+
+ Qt::Horizontal
+
+
+
+ -
+
+
+ 退出
+
+
+
+
+
+
+
+
diff --git a/Shell_NotifyIcon/resources.qrc b/Shell_NotifyIcon/resources.qrc
new file mode 100644
index 0000000..857ea33
--- /dev/null
+++ b/Shell_NotifyIcon/resources.qrc
@@ -0,0 +1,6 @@
+
+
+ app.ico
+ resources/gilr.jpg
+
+
diff --git a/Shell_NotifyIcon/resources/gilr.jpg b/Shell_NotifyIcon/resources/gilr.jpg
new file mode 100644
index 0000000..6a2dc4e
Binary files /dev/null and b/Shell_NotifyIcon/resources/gilr.jpg differ
diff --git a/Shell_NotifyIcon/widget.cpp b/Shell_NotifyIcon/widget.cpp
new file mode 100644
index 0000000..078df62
--- /dev/null
+++ b/Shell_NotifyIcon/widget.cpp
@@ -0,0 +1,56 @@
+#include "widget.h"
+#include "ui_widget.h"
+#include "menudialog.h"
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+
+Widget::Widget(QWidget *parent) :
+ QWidget(parent),
+ ui(new Ui::Widget)
+{
+ ui->setupUi(this);
+
+ setWindowFlags(Qt::Tool);
+ CreateSystemTrayIcon(); //创建托盘
+}
+
+Widget::~Widget()
+{
+ delete ui;
+}
+
+void Widget::CreateSystemTrayIcon()
+{
+
+ QWidgetAction *pWdtAction = new QWidgetAction(this);
+ MenuDialog * menudialog = new MenuDialog();
+ pWdtAction->setDefaultWidget(menudialog);
+
+ //初始化菜单并添加项
+ QMenu* trayMenu = new QMenu(this);//菜单
+ trayMenu->addAction(pWdtAction);
+
+ //创建一个系统托盘
+ QSystemTrayIcon* trayIcon = new QSystemTrayIcon(this);
+ trayIcon->setIcon(QIcon("://app.ico")); //设置托盘图标
+ trayIcon->setToolTip("Shell_Notify"); //鼠标放上去显示的东东
+ trayIcon->setContextMenu(trayMenu); //设置菜单
+ trayIcon->show();
+
+ connect(trayIcon, &QSystemTrayIcon::activated, this, [=](QSystemTrayIcon::ActivationReason reason)
+ {
+ menudialog->uodateNumbers();
+ if( reason == 3){ //鼠标左键
+ trayIcon->contextMenu()->exec(QCursor::pos());
+ }
+ });
+
+
+}
+
diff --git a/Shell_NotifyIcon/widget.h b/Shell_NotifyIcon/widget.h
new file mode 100644
index 0000000..95fb09f
--- /dev/null
+++ b/Shell_NotifyIcon/widget.h
@@ -0,0 +1,32 @@
+#ifndef WIDGET_H
+#define WIDGET_H
+
+#include
+#include
+#include
+#include
+
+namespace Ui {
+class Widget;
+}
+
+/**
+ * 主窗口
+ * @brief The Widget class
+ */
+
+
+class Widget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ explicit Widget(QWidget *parent = nullptr);
+ ~Widget();
+
+private:
+ Ui::Widget *ui;
+ void CreateSystemTrayIcon();
+};
+
+#endif // WIDGET_H
diff --git a/Shell_NotifyIcon/widget.ui b/Shell_NotifyIcon/widget.ui
new file mode 100644
index 0000000..8541684
--- /dev/null
+++ b/Shell_NotifyIcon/widget.ui
@@ -0,0 +1,20 @@
+
+ Widget
+
+
+
+ 0
+ 0
+ 400
+ 300
+
+
+
+ Widget
+
+
+
+
+
+
+