需要 #include <QSystemTrayIcon>
显示系统托盘win.h
#ifndef WIN_H#define WIN_H#include <QDialog>#include <QSystemTrayIcon> //系统托盘类#include <QDebug>class win : public QDialog{ Q_OBJECTpublic: win(QWidget *parent = nullptr); ~win();private: QSystemTrayIcon trayIcon;//创建系统托盘对象 int i=0;private slots: void trayIconhansu(QSystemTrayIcon::ActivationReason reason);};#endif // WIN_H
win.cpp
#include "win.h"win::win(QWidget *parent) : QDialog(parent){ this->resize(600,300); this->setWindowFlag(Qt::WindowMinimizeButtonHint); trayIcon.setToolTip("系统托盘练习");//设置系统托盘得提示 QString ss=trayIcon.toolTip(); // 返回系统托盘得提示 trayIcon.setIcon(QIcon(":/tu/tupian/home.png"));//设置图标 trayIcon.setVisible(true);//设置部件是否可见 QObject::connect(&trayIcon, &QSystemTrayIcon::activated,this,&win::trayIconhansu); qDebug()<<ss;}win::~win(){}void win::trayIconhansu(QSystemTrayIcon::ActivationReason reason){ //系统托盘一般都是单击显示窗口,右击显示菜单 if(reason==QSystemTrayIcon::Trigger) //如果动作是单击 { qDebug()<<"i="<<i; i++; this->showNormal();}}