Qt创建托盘图标
作者:武汉华嵌嵌入式培训中心 助理讲师:方世文在Qt中经常用到需要把正在运行的程序最小化到系统托盘中 ,以下是关于怎样实现这一功能的demo。
代码清单如下:
///////////////////////// demo.h文件 ////////////////////
#include <QtGui>
class Window: public QWidget
{
Q_OBJECT
public:
Window();
private:
void showMessage(char *msg);
void createActions();
void createTrayIcon();
QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu;
QAction *minimizeAction;
QAction *maximizeAction;
QAction *restoreAction;
QAction *quitAction;
private slots:
void showMessage();
void iconActivated(QSystemTrayIcon::ActivationReason reason);
};
//////////////////////////// demo.cpp 文件 //////////////////////
#include "demo.h"
Window::Window()
{
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
setMinimumSize(200,100);
//建立系统托盘图标
QIcon icon = QIcon("./images/tray.svg");
setWindowIcon(icon);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(icon);
trayIcon->setToolTip("a trayicon example");
createActions();
createTrayIcon();
trayIcon->show();
setWindowTitle(tr("Systray"));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
connect(button, SIGNAL(clicked()), this, SLOT(showMessage()));
}
void Window::showMessage()
{
QString titlec=tr("slot需要signal相同的参数进行传递");
QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
trayIcon->showMessage(titlec, textc, QSystemTrayIcon::Information, 5000);
}
void Window::showMessage(char *msg)
{
QString titlec=tr(msg);
QString textc=QString::fromLocal8Bit("测试内容单击、双击、中键、按钮");
trayIcon->showMessage(titlec, textc, QSystemTrayIcon::Information, 5000);
}
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
//触发后台图标执行相应事件
switch (reason)
{
case QSystemTrayIcon::Trigger:
showMessage("鼠标单击!");
break;
case QSystemTrayIcon::DoubleClick:
showMessage("鼠标双击!");
break;
case QSystemTrayIcon::MiddleClick:
showMessage("鼠标中键!");
break;
default:
break;
}
}
void Window::createActions()
{
minimizeAction = new QAction(tr("最小化 (&I)"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
maximizeAction = new QAction(tr("最大化 (&X)"), this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
restoreAction = new QAction(tr("还原 (&R)"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("退出 (&Q)"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void Window::createTrayIcon()
{
//设置右键点击时弹出的菜单
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
}
///////////////////////// main.cpp //////////////////////
#include "demo.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray on this system."));
return 1;
}
Window window;
window.show();
return app.exec();
}
就这样,我们实现了关闭窗口后,运行的程序隐藏到了系统托盘中。
更多技术文章敬请关注:武汉华嵌-嵌入式培训专家,国内领先的嵌入式服务机构,
http://www.embedhq.org --------------------编程问答-------------------- trayIcon->showMessage(titlec, textc, QSystemTrayIcon::Information, 5000);
这个执行之后,想让这个消息提示自己消失,有没有好的办法?消息出来之后貌似需要点击才能消失掉
补充:移动开发 , Qt