QT 托盘
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 26 27 28 29 30 31 | #include <QtGui> class myclass: public QWidget { public : myclass(); private : QSystemTrayIcon trayIcon; }; myclass::myclass() { QIcon icon = QIcon( “:/logo.png” ); trayIcon = new QSystemTrayIcon( this ); trayIcon->setIcon(icon); trayIcon->setToolTip(tr( “子曰USay” )); QString titlec=tr( “子曰USay” ); QString textc=tr( “子曰USay:给你神一般的体验” ); trayIcon->show(); trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000); } int main( int argc, char * argv) { QApplication testc(argc,argv); myclass newc; newc.show(); return testc.exec(); } |
接下来,添加一些代码,使托盘能够响应鼠标的单机双击时间,并且用鼠标右键单击托盘图标时,能够弹出操作菜单,并执行相应的操作,效果如下:
Qt实现托盘程序
实现起来非常简单,main函数没有什么变化,myclass类如下:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | class myclass: public QWidget { public : myclass(); private : QSystemTrayIcon trayIcon; QAction minimizeAction; QAction restoreAction; QAction quitAction; QMenu *trayIconMenu; private slots: void trayiconActivated(QSystemTrayIcon::ActivationReason reason); }; myclass::myclass() { //创建托盘图标 QIcon icon = QIcon( “:/logo.png” ); trayIcon = new QSystemTrayIcon( this ); trayIcon->setIcon(icon); trayIcon->setToolTip(tr( “子曰USay” )); QString titlec=tr( “子曰USay” ); QString textc=tr( “子曰USay:给你神一般的体验” ); trayIcon->show(); //弹出气泡提示 trayIcon->showMessage(titlec,textc,QSystemTrayIcon::Information,5000); //添加单/双击鼠标相应 connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this ,SLOT(trayiconActivated(QSystemTrayIcon::ActivationReason))); //创建监听行为 minimizeAction = new QAction(tr( “最小化 (&I)” ), this ); connect(minimizeAction, SIGNAL(triggered()), this , SLOT(hide())); 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())); //创建右键弹出菜单 trayIconMenu = new QMenu( this ); trayIconMenu->addAction(minimizeAction); trayIconMenu->addAction(restoreAction); trayIconMenu->addSeparator(); trayIconMenu->addAction(quitAction); trayIcon->setContextMenu(trayIconMenu); } void myclass::trayiconActivated(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::Trigger: //单击托盘图标 case QSystemTrayIcon::DoubleClick: //双击托盘图标 this ->showNormal(); this -> raise (); break ; default : break ; } } |
至此,大
还没有评论,来说两句吧...