Qt5 托盘模仿qq闪烁,弹消息框实现
在别人代码基础上做的,课设刚好用上了,贴出来分享Qt5.5.1实现。
图片自己找。
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QTimer> #include <QMenu> #include <QCloseEvent> #include <QMessageBox> #include <QSystemTrayIcon> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: Dialog(QWidget *parent = 0); ~Dialog(); void InitUi(); void InitTray(); //初始化托盘 void InitLayout(); void closeEvent(QCloseEvent *); //在关闭窗口时候要重写该函数,为了最小化到托盘,而不是退出程序 void ShowMessageBox(); //像是托盘图表的messagebox(); protected: void changeEvent(QEvent *e); private: Ui::Dialog *ui; QSystemTrayIcon *tray; QTimer *timer; //用于闪烁ICON的定时器 int TimerCount; //用于计算定时器超时次数,单数显示图标,双数不显示 并且为0时候表示没有消息 private slots: void MenuExit(); //右键菜单 退出 void ShowNormal(); //正常显示 void iconIsActived(QSystemTrayIcon::ActivationReason); //托盘图表活动,无论是鼠标悬浮,或者双击,或者单击 void ShowClickMsg(); //点击了消息框后的响应函数 void qq_msg_com(); void updateIcon(); //定时器刚 }; #endif // DIALOG_H
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); InitUi(); InitTray(); InitLayout(); } Dialog::~Dialog() { delete ui; } void Dialog::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } } //初始化部件 void Dialog::InitUi() { timer=new QTimer(this); TimerCount=0; //初始化为零 } //初始化托盘 void Dialog::InitTray() { QMenu *contexmenu=new QMenu(this); //注意右键菜单的父对象,在窗口销毁后要把菜单也销毁掉 QAction *shut=new QAction("quit",this); //菜单中两个动作 QAction *openNomal=new QAction("show normal",this); QAction *qqmsg=new QAction("QQ Msg",this); contexmenu->addAction(shut); contexmenu->addSeparator(); contexmenu->addAction(openNomal); contexmenu->addSeparator(); contexmenu->addAction(qqmsg); connect(shut,SIGNAL(triggered()),this,SLOT(MenuExit())); //点击了这两个菜单 connect(openNomal,SIGNAL(triggered()),this,SLOT(ShowNormal())); connect(qqmsg,SIGNAL(triggered()),this,SLOT(qq_msg_com())); tray=new QSystemTrayIcon(this); tray->setToolTip("try to click"); //放在图表上时候显示 tray->setContextMenu(contexmenu); tray->setIcon(QIcon("image/icon.png")); tray->show(); connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconIsActived(QSystemTrayIcon::ActivationReason))); connect(tray,SIGNAL(messageClicked()),this,SLOT(ShowClickMsg())); //模拟QQ消息到来时候的闪烁图表 } //初始化布局 void Dialog::InitLayout() {} //相应关闭窗口消息函数 void Dialog::closeEvent(QCloseEvent *event) { if(tray->isVisible()) { QMessageBox::about(this,tr("最小化"),tr("最小化到托盘")); this->hide(); event->ignore(); } } //响应右键菜单正常显示 void Dialog::ShowNormal() { timer->stop(); tray->setIcon(QIcon(":/new/prefix1/2")); //正常显示时候恢复原有图标,防止定时器在无显示图表时候被终止 this->show(); } //响应右键菜单 关闭程序 void Dialog::MenuExit() { exit(0); } //托盘图标活动l void Dialog::iconIsActived(QSystemTrayIcon::ActivationReason reason) { switch(reason) { case QSystemTrayIcon::DoubleClick: ShowNormal(); break; case QSystemTrayIcon::Trigger: ShowMessageBox(); break; case QSystemTrayIcon::Unknown: QMessageBox::about(this,"unkown","unkown activation"); break; default: break; } } void Dialog::ShowMessageBox() { QSystemTrayIcon::MessageIcon icon=QSystemTrayIcon::MessageIcon(1); //设置图表是标准的系统托盘 信息 tray->showMessage("you click","hello,tray",icon,10000); } //点击了消息框后的要响应的函数 void Dialog::ShowClickMsg() { QMessageBox::about(0,"click","you click the messagebox"); ShowNormal(); } //模拟QQ消息到来时候,托盘图表闪动 void Dialog::qq_msg_com() { timer->start(500); //每500ms都刷新一次 timer->setSingleShot(false); //如果为真,表示只重复一次,为假表示无限次循环 connect(timer,SIGNAL(timeout()),this,SLOT(updateIcon())); } //刷新托盘图标 void Dialog::updateIcon() { TimerCount++; if(TimerCount%2) { tray->setIcon(QIcon(":/new/prefix1/0")); //实际上没有这个图标,然后会显示没有图表 } else { tray->setIcon(QIcon(":/new/prefix1/2")); } }
#include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }