QT如何实现后台运行(即最小化到右下角托盘)
作者:互联网
https://blog.csdn.net/mingxinke/article/details/46547521
第一期:
用hello world简单的代码更容易看懂托盘的具体实现,一些细节问题,比如为什么提示消息没有显示在托盘上,如何在tip消息上增加图标等。
MyQWidget.h文件:
#ifndef MYCLASS_H #define MYCLASS_H #include <QtGui> #include <QWidget> #include <QPushButton> #include <QSystemTrayIcon> class MyQWidget: public QWidget { public: MyQWidget(); private: QPushButton *b1; QSystemTrayIcon *trayIcon; }; #endif // MYCLASS_H
MyQWidget.cpp文件:
#include "MyQWidget.h" #include<QTextCodec> #include<QIcon> #include<QPushButton> MyQWidget::MyQWidget() { //QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312")); this->setMinimumSize(200,100); this->setMaximumSize(200,100); b1=new QPushButton("hello world",this); b1->setGeometry(20,20,160,40); QIcon icon = QIcon(":/img/gangtiexia.ico"); setWindowIcon(icon); trayIcon = new QSystemTrayIcon(this); trayIcon->setIcon(icon); trayIcon->setToolTip("a trayicon example"); QString titlec=tr("Hello"); QString textc=QString::fromLocal8Bit("Hello QT?"); trayIcon->show(); trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000); setWindowTitle(tr("Systray")); }
效果如下:
标签:QT,右下角,MyQWidget,trayIcon,QSystemTrayIcon,b1,最小化,MYCLASS,include 来源: https://www.cnblogs.com/Oldz/p/13087549.html