鱼C论坛

 找回密码
 立即注册
查看: 4564|回复: 0

[技术原创] 将应用程序缩小到右下角任务栏的系统托盘内 - C/C++进阶之Qt图形UI库

[复制链接]
发表于 2017-3-27 14:31:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
简述
初尝QT不甚欢喜,鱼C上有许多的朋友在为QT做出贡献,我也愿意以主观角度来分享QT,让QT变得更好。
该源码系作者个人撰写,使用时请尊重作者,不要随意篡改关于作者的信息。
版权所有:CSND_Ayo,转载请注明出处:http://blog.csdn.net/csnd_ayo

帖子原址:http://blog.csdn.net/csnd_ayo/article/details/56004234

源码

  1. #ifndef CUSTOMSYSTEMTRAYICON_H  
  2. #define CUSTOMSYSTEMTRAYICON_H  
  3.   
  4. #include <QSystemTrayIcon>  
  5. #include <QWidget>  
  6.   
  7. /*
  8. * 自定义系统托盘类
  9. * 作者:陈鲁勇
  10. * 邮箱:727057301@qq.com
  11. * 创建时间:2017年2月10日16:26:48
  12. * QT版本:5.0.2
  13. * CSDN:http://blog.csdn.net/csnd_ayo
  14. * **************************************
  15. * 说明:
  16. *   第一次使用instance时必须传入主窗口的指针
  17. *   以保证托盘与程序相关联,使用前请确保在QT.pro中加入 C++11 的支持
  18. *
  19. * 示例代码:

  20.     icon_ = CustomSystemTrayIcon::instance(this);
  21.     icon_->setTray("托盘名字","托盘图标资源地址",
  22.                    "托盘描述");
  23.     // 显示托盘
  24.     icon_->showTray();
  25.     // 隐藏托盘
  26.     //icon_->hideTray();
  27. */  
  28.   
  29. class QAction;  
  30. class QMenu;  
  31.   
  32. class CustomSystemTrayIcon : public QWidget  
  33. {  
  34.     Q_OBJECT  
  35.   
  36. public:  
  37.   
  38.     // 得到系统托盘实例  
  39.     static CustomSystemTrayIcon* instance(QWidget* _this = nullptr);  
  40.   
  41.     /*
  42.      * 设置托盘
  43.      * 参数 _name:托盘标题
  44.      * 参数 _icon:托盘图标
  45.      * 参数 _describe:托盘描述
  46.      *
  47.          图标建议大小 20~40
  48.     */  
  49.     void setTray(const QString& _name,  
  50.                  const QString& _icon,  
  51.                  const QString& _describe);  
  52.     void setTray(const QString& _name,  
  53.                  const QString& _icon);  
  54.     void setTray(const QString& _name);  
  55.   
  56.     // 显示托盘  
  57.     void showTray(void);  
  58.     // 隐藏托盘  
  59.     void hideTray(void);  
  60.   
  61. private:  
  62.   
  63.     explicit CustomSystemTrayIcon(QWidget *parent = 0);  
  64.     // 初始化  
  65.     void init(void);  
  66.   
  67.     // 初始化右键菜单  
  68.     void initMenu(void);  
  69.   
  70.     // 初始化托盘  
  71.     void initTray(void);  
  72.   
  73.     // 初始化信号  
  74.     void initSignal(void);  
  75.   
  76. private slots:  
  77.   
  78.     /*
  79.      * 托盘点击事件
  80.      * 参数 _action:具体的事件行为宏
  81.     */  
  82.     void trayActivatedEvent(QSystemTrayIcon::ActivationReason _action);  
  83.   
  84.   
  85. protected:  
  86.   
  87.     // 托盘描述内容  
  88.     QString trayDescribe_;  
  89.   
  90. private:  
  91.   
  92.     // 隐藏按钮(菜单)  
  93.     QAction* hideAction_ = nullptr;  
  94.     // 还原按钮(菜单)  
  95.     QAction* restoreAction_ = nullptr;  
  96.     // 退出按钮(菜单)  
  97.     QAction* quitAction_ = nullptr;  
  98.     // 根菜单  
  99.     QMenu* menu_ = nullptr;  
  100.     // 托盘实例  
  101.     QSystemTrayIcon* trayObject_ = nullptr;  
  102.     // 实例  
  103.     static CustomSystemTrayIcon* self_;  
  104.   
  105. };  
  106.   
  107. #endif // CUSTOMSYSTEMTRAYICON_H  
复制代码




  1. #include <QApplication>  
  2. #include <QtGui>  
  3. #include <QMenu>  
  4. #include <QAction>  
  5.   
  6. #ifndef QT_NO_DEBUG  
  7. #include <QMessageBox>  
  8. #endif  
  9.   
  10. #include "customsystemtrayicon.h"  
  11.   
  12.   
  13. CustomSystemTrayIcon* CustomSystemTrayIcon::self_ = nullptr;  
  14.   
  15.   
  16. CustomSystemTrayIcon::CustomSystemTrayIcon(QWidget *parent)  
  17.     : QWidget(parent) {  
  18.     init();  
  19. }  
  20.   
  21. void CustomSystemTrayIcon::setTray(const QString &_name,  
  22.                                    const QString &_icon,  
  23.                                    const QString &_describe) {  
  24.     setTray(_name,_icon);  
  25.     trayDescribe_ = _describe;  
  26. }  
  27.   
  28. void CustomSystemTrayIcon::setTray(const QString &_name,  
  29.                                    const QString &_icon) {  
  30.     setTray(_name);  
  31.     trayObject_->setIcon(QIcon(_icon));  
  32. }  
  33.   
  34. void CustomSystemTrayIcon::setTray(const QString &_name) {  
  35.     trayObject_->setToolTip(_name);  
  36. }  
  37.   
  38. void CustomSystemTrayIcon::showTray()  
  39. {  
  40.     trayObject_->show();  
  41.     trayObject_->showMessage(trayObject_->toolTip(),trayDescribe_,  
  42.                             QSystemTrayIcon::Information, 5000);  
  43. }  
  44.   
  45. void CustomSystemTrayIcon::hideTray()  
  46. {  
  47.     trayObject_->hide();  
  48. }  
  49.   
  50. CustomSystemTrayIcon* CustomSystemTrayIcon::instance(QWidget *_this)  
  51. {  
  52.     if (self_ == nullptr) {  
  53.         if (_this != nullptr) {  
  54.             self_ = new CustomSystemTrayIcon(_this);  
  55.         }  
  56.         else {  
  57. #ifndef QT_NO_DEBUG  
  58.             QMessageBox::critical(nullptr,  
  59.                    "警告","不符合要求的托盘操作代码");  
  60. #endif  
  61.             return nullptr;  
  62.         }  
  63.     }  
  64.     return self_;  
  65. }  
  66.   
  67. void CustomSystemTrayIcon::trayActivatedEvent(  
  68.         QSystemTrayIcon::ActivationReason _action) {  
  69.     switch(_action)  
  70.     {  
  71.     case QSystemTrayIcon::Unknown:  
  72.         trayObject_->showMessage(trayObject_->toolTip(),trayDescribe_,  
  73.                                 QSystemTrayIcon::Information, 5000);  
  74.         break;  
  75.     // 右键单击  
  76.     case QSystemTrayIcon::Context:  
  77.         break;  
  78.     // 点击  
  79.     case QSystemTrayIcon::Trigger:  
  80.         break;  
  81.     // 双击  
  82.     case QSystemTrayIcon::DoubleClick:  
  83.         this->parentWidget()->showNormal();  
  84.         break;  
  85.     // 中键点击  
  86.     case QSystemTrayIcon::MiddleClick:  
  87.         this->parentWidget()->hide();  
  88.         break;  
  89.     default:  
  90.         break;  
  91.     }  
  92. }  
  93.   
  94. void CustomSystemTrayIcon::init() {  
  95.     //判断系统是否支持系统托盘图标  
  96.     if (!QSystemTrayIcon::isSystemTrayAvailable()) {  
  97. #ifndef QT_NO_DEBUG  
  98.             QMessageBox::critical(nullptr,  
  99.                    "警告","当前系统不支持系统托盘");  
  100. #endif  
  101.         return;  
  102.     }  
  103.   
  104.     // 托盘对象  
  105.     trayObject_ = new QSystemTrayIcon(this);  
  106.     // 隐藏按钮(菜单)  
  107.     hideAction_ = new QAction("隐 藏(&Z)",this);  
  108.     // 还原按钮(菜单)  
  109.     restoreAction_ = new QAction("还 原(&X)",this);  
  110.     // 退出按钮(菜单)  
  111.     quitAction_ = new QAction("退 出(&C)",this);  
  112.     // 根菜单  
  113.     menu_ = new QMenu((QWidget*)QApplication::desktop());  
  114.   
  115.     initMenu();  
  116.     initTray();  
  117.     initSignal();  
  118. }  
  119.   
  120. void CustomSystemTrayIcon::initMenu()  
  121. {  
  122.     // 设置菜单图标  
  123. //    hideAction_->setIcon(QIcon(":/icon/hide"));  
  124. //    restoreAction_->setIcon(QIcon(":/icon/restore"));  
  125. //    quitAction_->setIcon(QIcon(":/icon/quit"));  
  126.   
  127.     // 组织菜单顺序  
  128.     menu_->addAction(hideAction_);  
  129.     menu_->addAction(restoreAction_);  
  130.     //加入一个分离符  
  131.     menu_->addSeparator();  
  132.     menu_->addAction(quitAction_);  
  133. }  
  134.   
  135. void CustomSystemTrayIcon::initTray()  
  136. {  
  137.     trayObject_->setIcon(QIcon(":/icon/logo"));   //设置图标图片  
  138.     //setWindowIcon(QIcon(":/ic/logo"));  //把图片设置到窗口上  
  139.     trayObject_->setToolTip("LuYong Chen");    //托盘时,鼠标放上去的提示信息  
  140.     trayObject_->showMessage("LuYong Chen","QQ:727057301",QSystemTrayIcon::Information,10000);  
  141.     // 设置托盘右键菜单  
  142.     trayObject_->setContextMenu(menu_);  
  143.     trayObject_->hide();  
  144. }  
  145.   
  146. void CustomSystemTrayIcon::initSignal()  
  147. {  
  148.     // 菜单项功能关联  
  149.     connect(hideAction_,SIGNAL(triggered()),  
  150.                   parentWidget(),SLOT(hide()));  
  151.     connect(restoreAction_,SIGNAL(triggered()),  
  152.                   parentWidget(),SLOT(showNormal()));  
  153.     connect(quitAction_,SIGNAL(triggered()),  
  154.                   qApp,SLOT(quit()));  
  155.   
  156.     // 托盘激活事件(点击、双击、滚轮点击)  
  157.     connect(trayObject_,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),  
  158.             this,SLOT(trayActivatedEvent(QSystemTrayIcon::ActivationReason)));  
  159. }  
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-28 02:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表