利用QSystemTrayIcon类来实现最小化托盘,并实现像QQ那样有消息闪烁图标的效果
但是程序运行到最后有个让我找了很久都没有找到的问题,就是当点击了图标的MessageBox()后,程序就自动退了了,并且exit with code 0
代码贴出来分享,也希望这个问题能在大家的帮助下找到原因
widget.h
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(); //定时器刚
}
Widget.cpp
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("image/icon.png")); //正常显示时候恢复原有图标,防止定时器在无显示图表时候被终止
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");
}
//模拟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("image/null.png")); //实际上没有这个图标,然后会显示没有图表
}
else
{
tray->setIcon(QIcon("image/icon.png"));
}
}
问题就出现在点击了消息框,发射信号messageClicked()
后,响应槽函数showClickMsg() 后程序就退出了......始终没有找到问题出现在哪里
关于QSystemTrayIcon类的介绍,参看 我来翻译QT类 中的http://blog.csdn.net/xie376450483/archive/2010/10/21/5956351.aspx