在QT中QSystemTrayIcon类提供了创建系统托盘程序的功能。

QSystemTrayIcon类为系统托盘中的应用程序提供图标。
现代操作系统通常会在桌面上提供一个称为系统托盘(system tray)或通知(notification)区域的特殊区域,其中长时间运行的应用程序可以显示图标和短消息。

QSystemTrayIcon类可以在以下平台上使用:

  • 所有受支持的Windows版本。
  • X11的所有窗口管理器和独立托盘实现,实现了XEmbed系统托盘规范。
  • 所有实现D-Bus的X11桌面环境规范,包括最新版本的KDE和Unity。
  • 所有受支持的macOS版本。

要检查用户桌面上是否存在系统托盘,请调用QSystemTrayIcon :: isSystemTrayAvailable()静态函数。

要添加系统托盘条目,请创建QSystemTrayIcon对象,调用setContextMenu()以提供图标的上下文菜单,并调用show()以使其在系统托盘中可见。可以使用showMessage()随时显示状态通知消息(“气球消息”)。

如果在构建系统托盘图标时系统托盘不可用,但稍后可用,则QSystemTrayIcon将自动为系统托盘中的应用程序添加条目(如果图标可见)。

当用户激活图标时,会发出activate()信号。

仅在X11上,当请求工具提示时,QSystemTrayIcon接收QEvent ::ToolTip类型的QHelpEvent。此外,QSystemTrayIcon接收QEvent :: Wheel类型的wheel事件。任何其他平台都不支持这些。

QT自带一个例子systray,可学习一下!

该程序是一个基于QDialog的应用程序,只有一个继承于QDialog的Window类。

主要代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
int main(int argc, char *argv[])
{
    // Initializes the resources specified by the .qrc file with the specified base name. Normally, when resources are built as part of the application, the resources are loaded automatically at startup. 
    // The Q_INIT_RESOURCE() macro is necessary on some platforms for resources stored in a static library.
    Q_INIT_RESOURCE(systray);
    QApplication app(argc, argv);
    // Returns true if the system tray is available; otherwise returns false.
    if (!QSystemTrayIcon::isSystemTrayAvailable())
    {
        QMessageBox::critical(, QObject::tr("Systray"),
                              QObject::tr("I couldn't detect any system tray  on this system."));
        ;
    }
    QApplication::setQuitOnLastWindowClosed(false);

Window window;
    window.show();
    return app.exec();
}

Window::Window()
{
    // Tray Icon Group Box(采用HBox布局)
    createIconGroupBox();
    // Ballon Message Group Box(采用Grid布局)
    createMessageGroupBox();
    iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
    // Tray right button menu
    createActions();
    // Create Tray Icon
    createTrayIcon();
    // 信号与槽的连接
    connect(showMessageButton, &QAbstractButton::clicked,
            this, &Window::showMessage);
    connect(showIconCheckBox, &QAbstractButton::toggled,
            trayIcon, &QSystemTrayIcon::setVisible);
    connect(iconComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &Window::setIcon);
    // Tray 信号槽
    connect(trayIcon, &QSystemTrayIcon::messageClicked,
            this, &Window::messageClicked);
    connect(trayIcon, &QSystemTrayIcon::activated,
            this, &Window::iconActivated);

QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(iconGroupBox);
    mainLayout->addWidget(messageGroupBox);
    setLayout(mainLayout);

iconComboBox->setCurrentIndex();
    trayIcon->show();

setWindowTitle(tr("Systray"));
    resize();
}

void Window::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(maximizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
}

// 重写closeEvent实现关闭时隐藏到托盘
void Window::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_OSX
    if (!event->spontaneous() || !isVisible())
    {
        return;
    }
#endif
    if (trayIcon->isVisible())
    {
        QMessageBox::information(this, tr("Systray"),
                                 tr("The program will keep running in the "
                                    "system tray. To terminate the program, "
                                    "choose <b>Quit</b> in the context menu "
                                    "of the system tray entry."));
        hide();
        event->ignore();
    }
}

// 重写setVisible实现右键菜单的动态变化
void Window::setVisible(bool visible)
{
    minimizeAction->setEnabled(visible);
    maximizeAction->setEnabled(!isMaximized());
    restoreAction->setEnabled(isMaximized() || !visible);
    QDialog::setVisible(visible);
}

// 单击或双击托盘图标动态改变图标,点击滚轮显示气泡信息
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
        iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + ) %
                                      iconComboBox->count());
        break;
    case QSystemTrayIcon::MiddleClick:
        showMessage();
        break;
    default:
        break;
    }
}

void Window::showMessage()
{
    showIconCheckBox->setChecked(true);
    QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
    if (msgIcon == QSystemTrayIcon::NoIcon)
    {
        QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
        trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, durationSpinBox->value() * );
    }
    else
    {
        trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, durationSpinBox->value() * );
    }
}

QT系统托盘应用程序的更多相关文章

  1. Qt系统托盘

    Qt的系统托盘的使用,可比mfc中好多了!他封装了一个专门的QSystemTrayIcon类,建立系统托盘图标.其实在Qt提供的示例程序已经很不错了,$QTDIR\examples\desktop\s ...

  2. qt系统托盘显示、无主窗体

    系统图盘是应用程序经常用到的一个控件,当应用程序需要长时间存在的时候,这个控件会变得非常有用,比如,窗口隐藏,显示,关于.关闭等接口都可以放在图盘中处理,今天与到一个问题,需求是这样的:只需要显示图盘 ...

  3. 用Qt写软件系列四:定制个性化系统托盘菜单

    导读 一款流行的软件,往往会在功能渐趋完善的时候,通过改善交互界面来提高用户体验.毕竟,就算再牛逼的产品,躲藏在糟糕的用户界面之后总会让用户心生不满.界面设计需综合考虑审美学.心理学.设计学等多因素, ...

  4. 【Qt编程】基于Qt的词典开发系列<十一>系统托盘的显示

    本文主要讨论Qt中的系统托盘的设置.系统托盘想必大家都不陌生,最常用的就是QQ.系统托盘以简单.小巧的形式能让人们较快的打开软件.废话不多说,下面开始具体介绍. 首先,新建一个Qt Gui项目,类型选 ...

  5. 【Qt开发】实现系统托盘,托盘菜单,托盘消息

    概述 系统托盘就是在系统桌面底部特定的区域显示运行的程序.windows在任务栏状态区域,linux在布告栏区域.应用程序系统托盘功能,是比较普遍的功能,本篇将详细的介绍如何实现该功能. 演示Demo ...

  6. QT中自定义系统托盘的实现—c++语言为例

    将要介绍的是:QT中自定义系统托盘(systemtray)的一个Demo,希望能帮需要的读者快速上手. 前提假设是诸位已经知道QT中的signals .slot以及资源文件,所以关于这些不会再累述. ...

  7. 使用QT创建系统托盘

    使用QT来创建一个系统托盘,事实上是一件很简单的事.为什么这么说?一是因为QT文档给出了比较详细的例子,二是QT的结构比较优雅,设计风格统一.但是在动手之前,我们要从哪里下手?虽然QT文档给出了一个比 ...

  8. C#编写WIN32系统托盘程序

    基本功能概述: 程序运行后驻留系统托盘,左键呼出,右键退出.后续可加右键菜单. 注册系统案件WIN+F10,呼出程序. 重写系统消息,最小化和关闭按钮隐藏程序 using System; using ...

  9. WP7系统托盘和应用程序栏

    (一)系统托盘和应用程序栏系统托盘(1)显示系统级别的状态信息(2)Apps能隐藏和显示系统托盘Micosoft.Phone.Shell.SystemTray.IsVisible=true;应用程序栏 ...

随机推荐

  1. 编程菜鸟的日记-初学尝试编程-编写函数实现strlen功能(总结考察点)

    //考察点1:输入参数加const int Mystrlen(const char *str) {//考察点2:断言字符串非0 assert(str!=NULL); int len=0;//考察点3: ...

  2. SourceTree安装教程和破解教程

    SourceTree破解版是一款非常实用的编程工具,这是一款专业的Git和Hg客户端,界面简洁,操作简单易上手,是开发者的必备工具,欢迎大家来绿色资源网下载体验!SourceTree是一款免费的Git ...

  3. linux shell中break和continue跳出循环

    到目前为止,我们已经看到了,创建循环和使用循环来完成不同的任务.有时候,你需要停止循环或跳过循环迭代. 在本教程中,您将了解以下两个语句用于控制 Shell 循环: break 语句 continue ...

  4. python之socket编程4

    1 socketserver实现并发 基于tcp的套接字,关键是两个循环,一个通信循环,一个链接循环 Socketserver的 模块中分成两类: Server类(解决连接问题) Request类(解 ...

  5. JS_高程6.面向对象的程序设计(1)理解对象

    js的数据属性:P139(1)[[Configurable]](2)[[Enumerable]](3)[[Writable]](4)[[Value]] 使用Object.definerPropert( ...

  6. 微信小程序中的单位

    vw:viewpoint width,视窗宽度,1vw等于视窗宽度的1%. vh:viewpoint height,视窗高度,1vh等于视窗高度的1%. rpx:rpx单位是微信小程序中css的尺寸单 ...

  7. Groovy 反射字符串常量方法

    Keywords: Groovy, Reflection, 反射 The Reflection of Groovy String constant style method. Groovy支持以下的方 ...

  8. javaScript系列 [02]-javaScript对象探析

    [02]-javaScript对象探析 题记:多年前,以非常偶然的方式关注了微信公众号“面向对象”,本以为这个公众号主要以分享面向对象编程的干货为主,不料其乃实实在在的猿圈相亲平台.通过查看公开资料, ...

  9. [CSS] Useful CSS tool for Web designer and developer

    1. Color Picker (Chrome) You might know how to use color picker in Chrome, recently there is a featu ...

  10. Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test java.lang.IllegalStateException

    不能找到对应的带有@SpringBootConfiguration 的类,你需要将它放在包的最顶层.