qt 生成系统托盘,加载图标

您所在的位置:网站首页 查询信鸽足环号和赛鸽成绩 qt 生成系统托盘,加载图标

qt 生成系统托盘,加载图标

2023-08-22 18:16| 来源: 网络整理| 查看: 265

1,下载ico图标文件 favicon.ico

2,新建txt文件,加入一行 IDI_ICON ICON DISCARDABLE "favicon.ico"

重命名为res.rc资源文件

3,工程文件中加入资源

 

4,重新构建,此时exe加上了图片

5,右键工程 添加资源文件

添加前缀 /

添加资源ico

 

6,进入设计模式,选中主窗口,添加资源文件

 

 

7,重新构建,此时任务栏图标出现

---------

托盘生成与托盘图标,.pro

QT += core gui QT += webenginewidgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 RC_FILE = res.rc # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #pragma execution_character_set("utf-8") SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.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 += \ img.qrc

 .h

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include "QWebEngineView" #include "qstackedlayout.h" #include "QNetworkProxyFactory" #include #include #include #include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; QWebEngineView *view; QSystemTrayIcon *m_sysTrayIcon; //系统托盘 void initSysTrayIcon(); QMenu *m_menu; //菜单 QAction *m_showMainAction; //动作 QAction *m_exitAppAction; //动作 void createActions(); void createMenu(); private slots: void on_showMainAction(); void on_exitAppAction(); protected: void closeEvent (QCloseEvent *event) override; }; #endif // MAINWINDOW_H

.cpp

#include "mainwindow.h" #include "ui_mainwindow.h" #include "qmessagebox.h" #include "qstring.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); view = new QWebEngineView(this); view->load(QUrl("https://haokan.baidu.com/v?pd=wisenatural&vid=2371613814685302423")); view->setParent(ui->centralwidget); initSysTrayIcon(); } MainWindow::~MainWindow() { delete ui; } //创建系统托盘 void MainWindow::initSysTrayIcon() { //隐藏程序主窗口 this->hide(); //新建QSystemTrayIcon对象 m_sysTrayIcon = new QSystemTrayIcon(this); //设置托盘图标 QIcon icon = QIcon(":/favicon.ico"); //资源文件添加的图标 m_sysTrayIcon->setIcon(icon); //当鼠标移动到托盘上的图标时,会显示此处设置的内容 m_sysTrayIcon->setToolTip(QStringLiteral("托盘提示信息")); //给QSystemTrayIcon添加槽函数 connect(m_sysTrayIcon, &QSystemTrayIcon::activated, [=](QSystemTrayIcon::ActivationReason reason) { switch(reason) { case QSystemTrayIcon::Trigger: //单击托盘图标 m_sysTrayIcon->showMessage(QStringLiteral("Message Title"),QStringLiteral("欢迎使用此程序"), QSystemTrayIcon::Information,1000); break; case QSystemTrayIcon::DoubleClick: //双击托盘图标 //双击后显示主程序窗口 this->show(); break; default: break; } }); //建立托盘操作的菜单 createActions(); createMenu(); //在系统托盘显示此对象 m_sysTrayIcon->show(); } //创建动作 void MainWindow::createActions() { m_showMainAction = new QAction(QStringLiteral("关于系统"), this); connect(m_showMainAction,SIGNAL(triggered()),this,SLOT(on_showMainAction())); m_exitAppAction = new QAction(QStringLiteral("退出"), this); connect(m_exitAppAction,SIGNAL(triggered()),this,SLOT(on_exitAppAction())); } //创建托盘菜单 void MainWindow::createMenu() { m_menu = new QMenu(this); //新增菜单项---显示主界面 m_menu->addAction(m_showMainAction); //增加分隔符 m_menu->addSeparator(); //新增菜单项---退出程序 m_menu->addAction(m_exitAppAction); //把QMenu赋给QSystemTrayIcon对象 m_sysTrayIcon->setContextMenu(m_menu); } //当在系统托盘点击菜单内的显示主界面操作 void MainWindow::on_showMainAction() { QApplication::setQuitOnLastWindowClosed( false ); QMessageBox::information(nullptr, QStringLiteral("xxxx"), QStringLiteral("xxxx有限公司copyright©2022")); } //当在系统托盘点击菜单内的退出程序操作 void MainWindow::on_exitAppAction() { qApp->exit(); } //关闭事件 void MainWindow::closeEvent(QCloseEvent *event) { //忽略窗口关闭事件 QApplication::setQuitOnLastWindowClosed( true ); this->hide(); event->ignore(); }

. ui

MainWindow 0 0 800 600 MainWindow :/favicon.ico:/favicon.ico 0 0 800 23

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3