话不多说,直接上代码
loginDialog::loginDialog(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
system_tray = new QSystemTrayIcon();
//放在托盘提示信息、托盘图标
system_tray->setToolTip(QString("我就是托盘"));
system_tray->setIcon(QIcon("./pic.png"));
//点击托盘执行的事件
connect(system_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(showWidget(QSystemTrayIcon::ActivationReason)));
//显示托盘
system_tray->show();
//托盘显示提示信息
//system_tray->showMessage(QString("托盘标题"), QString("托盘显示内容"));
}
构造函数中创建一个托盘。并给托盘连接信号,对托盘的单击事件,右击事件等。
void loginDialog::closeEvent(QCloseEvent * event)
{
QMessageBox button(QMessageBox::Warning, QString(tr("退出程序")), QString(tr("确认退出程序!")));
button.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
button.setSizeIncrement(250, 120);
button.setButtonText(QMessageBox::Yes, QString("确定退出"));
button.setButtonText(QMessageBox::No, QString("缩小托盘"));
if (button.exec() != QMessageBox::Yes)
{
this->hide();
event->ignore();
}
else {
event->accept();
}
}
在closeEvent()事件中,判断是直接退出还是以托盘显示。
void loginDialog::showWidget(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
//单击托盘图标,显示窗体
this->show();
break;
case QSystemTrayIcon::DoubleClick:
//双击托盘图标
//双击后显示主程序窗口
QMessageBox::information(this, "xxx", "xxxxxx");
break;
case QSystemTrayIcon::Context:
//右击事件
showMenu();
break;
default:
break;
}
}
托盘事件的处理,这里右击是显示一个菜单。
这里的菜单类的实现如下:
#include <QAction>
#include <QMenu>
#include <QSystemTrayIcon>
class TrayMenu : public QMenu
{
Q_OBJECT
public:
TrayMenu();
~TrayMenu();
void createActions();
public slots:
private:
QAction * action_show;
QAction * action_quit;
QAction * action_login_home;
QAction * action_help;
QAction * action_about;
QAction * action_check_update;
QAction * action_setting;
};
#include "TrayMenu.h"
#include <QMessageBox>
#include <QMouseEvent>
#pragma execution_character_set("utf-8")
TrayMenu::TrayMenu()
{
}
TrayMenu::~TrayMenu()
{
}
void TrayMenu::createActions()
{
//创建托盘项
action_show = new QAction(this);
action_quit = new QAction(this);
action_login_home = new QAction(this);
action_help = new QAction(this);
action_about = new QAction(this);
action_check_update = new QAction(this);
action_setting = new QAction(this);
//设置托盘项图标
action_show->setIcon(QIcon(":/icon/open"));
action_login_home->setIcon(QIcon(":/icon/home"));
action_help->setIcon(QIcon(":/icon/help"));
action_about->setIcon(QIcon(":/icon/about"));
action_check_update->setIcon(QIcon(":/icon/update"));
action_setting->setIcon(QIcon(":/icon/set"));
action_quit->setIcon(QIcon(":/icon/quit"));
//设置托盘想文本
action_show->setText(QString("显示"));
action_quit->setText(QString("退出"));
action_login_home->setText(QString("登录网页"));
action_help->setText(QString("帮助"));
action_about->setText(QString("关于"));
action_check_update->setText(QString("检查更新"));
action_setting->setText(QString("设置"));
//添加菜单项
this->addAction(action_show);
this->addAction(action_setting);
this->addAction(action_login_home);
this->addSeparator();
this->addAction(action_help);
this->addAction(action_about);
this->addAction(action_check_update);
this->addSeparator();
this->addAction(action_quit);
action_setting->setVisible(false);
//设置信号连接(这里仅列举连接显示窗口的信号)
//QObject::connect(action_show, SIGNAL(triggered(bool)), this, SIGNAL(showWidget()));
}