• QSystemTrayIcon——实现系统托盘


    QSystemTrayIcon类

    QSystemTrayIcon

    QSystemTrayIcon是qt中专门用于处理系统托盘的类

    头文件#include
    qmakeQT += widgets
    SinceQt 4.2
    InheritsQObject

    QSystemTrayIcon 支持Windows、macos和部分Linux桌面环境,可以通过 QSystemTrayIcon::isSystemTrayAvailable()方法来判断系统是否支持托盘。
    要添加系统托盘项,首先,需要创建一个QSystemTrayIcon对象,并调用setContextMenu()为图标提供上下文菜单,然后,调用show()使其在系统托盘中可见。状态通知消息(“气球消息”)可以在任何时候使用showMessage()来进行显示。
    当用户激活托盘图标时,activated()信号会被发射。
    只有在X11上时,当发出一个tooltip请求时,QSystemTrayIcon会接收一个QEvent::ToolTip类型的QHelpEvent事件。此外,QSystemTrayIcon会接收QEvent::Wheel类型的滚轮事件。这些都是不支持任何其它平台。

    Public Types

    enum ActivationReason { Unknown, Context, DoubleClick, Trigger, MiddleClick }
    此枚举描述了系统托盘被激活的原因。

    ConstantValueDescription
    QSystemTrayIcon::Unknown0Unknown reason
    QSystemTrayIcon::Context1The context menu for the system tray entry was requested
    QSystemTrayIcon::DoubleClick2The system tray entry was double clicked.
    QSystemTrayIcon::Trigger3The system tray entry was clicked
    QSystemTrayIcon::MiddleClick4The system tray entry was clicked with the middle mouse button

    enum MessageIcon { NoIcon, Information, Warning, Critical }
    此枚举描述了显示气球消息时所显示的图标。

    ConstantValueDescription
    QSystemTrayIcon::NoIcon0No icon is shown.
    QSystemTrayIcon::Information1An information icon is shown.
    QSystemTrayIcon::Warning2A standard warning icon is shown.
    QSystemTrayIcon::Critical3A critical warning icon is shown.

    Properties

    icon : QIcon
    这个属性保存了系统托盘的图标。
    在Windows中,系统任务栏图标的大小是16×16;X11中,首选大小为22x22。必要时该图标将被调整到合适大小。
    可以通过以下方法获取:
    QIcon icon() const
    void setIcon(const QIcon &icon)

    toolTip : QString
    这个属性保存了系统托盘的提示信息。
    在一些系统中,tooltip的长度是有限的,在必要时tooltip将被截断。
    可以通过以下方法获取:
    QString toolTip() const
    void setToolTip(const QString &tip)

    visible : bool
    这个属性保存了该托盘图标入口是否可见
    通过设置该属性为true 或者调用show()来使得托盘图标可见;设置该属性为false或者调用hide()来隐藏托盘图标。
    通过以下方法可以获取/修改:
    bool isVisible() const
    void setVisible(bool visible)

    Public Functions

    QSystemTrayIcon(const QIcon &icon, QObject *parent = nullptr)
    创建一个指定图标和父对象的QSystemTrayIcon 对象,图标初始状态是不可见的。
    QSystemTrayIcon(QObject *parent = nullptr)
    创建一个指定父对象的QSystemTrayIcon 对象,图标初始状态是不可见的。
    virtual ~QSystemTrayIcon()
    Removes the icon from the system tray and frees all allocated resources.
    QMenu * contextMenu() const
    返回系统托盘的当前上下文菜单。
    QRect geometry() const
    返回系统托盘图标在屏幕上的几何坐标。 Qt 4.3.
    QIcon icon() const
    获取托盘图标
    bool isVisible() const
    获取托盘可见属性
    void setContextMenu(QMenu *menu)
    设置指定菜单为系统托盘的上下文菜单。
    当用户通过点击鼠标请求系统托盘的上下文菜单时,菜单会弹出。
    在OS X中,一般转换为一个NSMenu,所以aboutToHide()信号不会发出。

    注意:系统托盘菜单并不对菜单有所有权,必须确保在恰当的时候删除菜单,例如:创造一个具有合适父对象的菜单。

    void setIcon(const QIcon &icon)
    设置托盘图标
    void setToolTip(const QString &tip)
    设置系统托盘的提示信息。
    QString toolTip() const
    获取系统托盘的提示信息。

    Public Slots

    void hide()
    隐藏系统托盘。
    void setVisible(bool visible)
    设置系统托盘是否可见。
    设置为true或调用show()使系统托盘图标可见;设置为false或调用hide()隐藏它。
    void show()
    显示系统托盘。
    void showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int millisecondsTimeoutHint = 10000)
    显示一个气球消息,使用所给出的标题、消息、图标和指定的时间,标题和消息必须是纯文本字符串。
    消息可以被用户点击,当用户点击时发出messageClicked()信号 。
    Note that display of messages are dependent on the system configuration and user preferences, and that messages may not appear at all. Hence, it should not be relied upon as the sole means for providing critical information.
    On Windows, the millisecondsTimeoutHint is usually ignored by the system when the application has focus.
    Has been turned into a slot in Qt 5.2.
    This function was introduced in Qt 4.3.

    void showMessage(const QString &title, const QString &message, const QIcon &icon, int millisecondsTimeoutHint = 10000)

    This function overloads showMessage().

    Shows a balloon message for the entry with the given title, message, and custom icon icon for the time specified in millisecondsTimeoutHint.

    This function was introduced in Qt 5.9.

    Signals

    void activated(QSystemTrayIcon::ActivationReason reason)
    当用户激活系统托盘图标,这个信号被发射。reason指定激活的原因, QSystemTrayIcon::ActivationReason列举了各种原因。

    void messageClicked()
    当使用showMessage()显示的消息被用户点击时,此信号被发射。

    Note: We follow Microsoft Windows behavior, so the signal is also emitted when the user clicks on a tray icon with a balloon message displayed.

    Static Public Members

    bool isSystemTrayAvailable()
    如果系统托盘可用,返回true;否则,返回false。
    如果系统盘是当前不可用,但以后变为可用,若QSystemTrayIcon可见,它就会自动在系统托盘中添加条目。

    bool supportsMessages()
    如果系统托盘支持气球消息,则返回true;否则,返回false。

    Reimplemented Protected Functions

    virtual bool event(QEvent *e) override

    Reimplements: QObject::event(QEvent *e).

    使用

    main.cpp

    #include "widget.h"
    
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
        w.setupSystemTray();
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    #include 
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    
        void setupSystemTray();
    
    private:
        QSystemTrayIcon *trayIcon = nullptr;
    };
    #endif // WIDGET_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    widget.cpp

    #include "widget.h"
    #include 
    #include 
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setWindowTitle(tr("系统托盘"));
        resize(400, 400);
        move((QApplication::desktop()->width() - width())/2,
             (QApplication::desktop()->height() - height())/2);
    }
    
    Widget::~Widget()
    {
    }
    
    void Widget::setupSystemTray()
    {
        trayIcon = new QSystemTrayIcon(this);
        trayIcon->setIcon(QIcon(":/tray.png"));
        trayIcon->show();
    }
    
    
    
    • 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

    效果:
    在这里插入图片描述

  • 相关阅读:
    杰理强制升级工具4.0使用和原理解析
    数据抓取可以应用到哪些行业
    Linux高性能服务器编程 学习笔记 第九章 IO复用
    【百战GAN】SRGAN人脸低分辨率老照片修复代码实战
    关于 Hypervisor的理解
    SpringCloud Nacos + Ribbon 调用服务的 2 种方法!
    ExtJS - UI组件 - Grid
    马斯克搞脑机得“开瓢”?MIT 早在研究「挂耳式耳机」,戴上=“把整个互联网装进脑子”!...
    单目结构光三维重建平面不平整
    CSDN博客编写的相关操作
  • 原文地址:https://blog.csdn.net/cclethe/article/details/127834613