QSystemTrayIcon类学习

 

代码来自“QT小神童”视频

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <QSystemTrayIcon>
#include <QTimer>
#include <QLabel>
#include <QPixmap>
#include <QApplication>
#include <QDesktopWidget>
#include <QList>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
protected slots:
    void flash();
    void handle(QSystemTrayIcon::ActivationReason);
    void showPic();
private:
    QSystemTrayIcon *sysIcon;
    bool flag;
    QTimer *timer2;
    QLabel *label;
    QList<QLabel *> list;
    int i;
    int j;
};

#endif // WIDGET_H

实现文件

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    flag=0;
    i=0;
    j=0;
    sysIcon=new QSystemTrayIcon;
   // sysIcon->setIcon(QIcon(":/heart.png"));
    sysIcon->show();
    connect(sysIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this,SLOT(handle(QSystemTrayIcon::ActivationReason)));
    QTimer *timer1=new QTimer(this);
    timer1->start(500);
    connect(timer1,SIGNAL(timeout()),this,SLOT(flash()));
    timer2=new QTimer(this);
    connect(timer2,SIGNAL(timeout()),this,SLOT(showPic()));
}

Widget::~Widget()
{

}

void Widget::flash()
{
    switch(flag)
    {
    case 0:{sysIcon->setIcon(QIcon(":/heart.png"));
            flag=1;
            break;
        }
    case 1:{sysIcon->setIcon(QIcon(":/123.png"));
        flag=0;
        break;
        }
    }
}

void Widget::handle(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
    case QSystemTrayIcon::DoubleClick:
        timer2->start(1000);
    }
}

void Widget::showPic()
{
    label=new QLabel;
    label->setWindowFlags(Qt::FramelessWindowHint|Qt::SubWindow);
    label->setAttribute(Qt::WA_DeleteOnClose);
    QPixmap pix(":/heart.png");
    label->setPixmap(pix);
    label->resize(pix.rect().width(),pix.rect().height());

 

    if(i*label->width()>QApplication::desktop()->screenGeometry().width())
    {
        i=0;
        j++;
    }
    if((j+1)*label->height()>QApplication::desktop()->screenGeometry().height())
    {
        j=0;

        for(int count=0;count< list.size();count++)
            list[count]->close();
        list.clear();
    }
    list.append(label);
    label->setGeometry(0+i*label->width(),0+j*label->height(),label->width(),label->height());
    label->show();
    i++;

}

 

主程序文件

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

资源文件

 

 


 

pyqt5的QSystemTrayIcon是一个用于在系统托盘中显示图标和提供相关功能的。它可以让你的应用程序在后台运行,并通过系统托盘图标提供一些交互功能,例如全局热键唤出和ESC键隐藏等。 在使用pyqt5创建系统托盘应用程序的过程中,你需要使用QSystemTrayIcon的实例,并将其与你的主窗口关联起来。你可以通过调用show()方法来显示系统托盘图标。 通过QSystemTrayIcon,你可以为系统托盘图标添加一些菜单项,使用户可以进行一些操作。例如,在菜单项上设置一些动作,当用户点击菜单项时,会触发相应的操作。你还可以通过设置tootip来显示自定义的提示信息。 总结起来,pyqt5的QSystemTrayIcon可以让你的应用程序在系统托盘中显示图标,并提供一些常用操作,例如全局热键唤出和ESC键隐藏等。你可以通过添加菜单项和设置tootip来增加图标的交互性和功能性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [利用pyqt5实现常驻Windows系统托盘并利用全局热键唤出的程序](https://download.csdn.net/download/weixin_38740130/13755202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [pyqt5 关闭程序显示提示信息+最小化程序到托盘+隐藏任务栏图标](https://blog.csdn.net/marwenx/article/details/107226987)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值