Qt中如何使窗口隐藏/最小化到托盘
Qt中如何实现自己的托盘功能,Qt自带的demo也很明了,这里我们就来实现自己的一个托盘。。。
功能:窗口最小化后或者点击“hide”按钮将窗口隐藏到托盘。
通常最小化的窗口会在任务栏上显示一个按钮。当用户按一下这个按钮,窗口就还原了。这是一个被大多数桌面环境,比如Windows,KDE,GNOME所采用的普遍设计。不过,因为任务栏通常只是桌面边上一小行,容纳不了很多按钮,用户通常希望把那些不常用的程序隐藏起来,只在通知栏显示一个小图标,要使用的时候再点击那个小图标恢复窗口。这种作法最典型的例子是QQ和Winamp。
最基本的思路是拦截窗口的最小化事件changeEvent(),通过重写changeEvent()即可。
“hide”按钮的方式,是通过信号--槽来实现。
--------------------------------------------------------------------
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"));
//加载图片和提示
QIcon icon(":/images/sysTray.png"); systemTray = new QSystemTrayIcon(this); systemTray->setIcon(icon); systemTray->setToolTip("Hello, this is system tray!");
注意:这里特别提醒,如果你的应用程序要移植到别的机器上,而且这个机器没有装Qt,QIcon的图片格式最好用png,而不要用ico等格式,否则托盘图标不能显示,会很麻烦。
//设置托盘想文本
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()));
}
LoginDialog::LoginDialog(QWidget *parent) : QDialog(parent)
{
QSystemTrayIcon *system_tray = new QSystemTrayIcon();
//放在托盘提示信息、托盘图标
system_tray ->setToolTip(QString("我就是托盘"));
system_tray ->setIcon(QIcon(":/icon/login"));
TrayMenu *tray_menu = new TrayMenu();
system_tray->setContextMenu(tray_menu);
//点击托盘执行的事件
connect(system_tray , SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
connect(tray_menu, SIGNAL(showWidget()), this, SLOT(showNormal()));
//显示托盘
system_tray->show();
//托盘显示提示信息
system_tray->showMessage(QString("托盘标题"), QString("托盘显示内容"));
}
void LoginDialog::iconIsActived(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
//点击托盘显示窗口
case QSystemTrayIcon::Trigger:
{
showNormal();
break;
}
//双击托盘显示窗口
case QSystemTrayIcon::DoubleClick:
{
showNormal();
break;
}
default:
break;
}
}
OK,这样我们的托盘功能就Over了!
=============================================================================
另://关闭到托盘
void Widget::closeEvent(QCloseEvent *e)
{
e->ignore();
this->hide();
}
//最小化到托盘
void Widget::changeEvent(QEvent *e)
{
if((e->type()==QEvent::WindowStateChange)&&this->isMinimized())
{
QTimer::singleShot(100, this, SLOT(close()));
}
}
或者(me)
//点击窗口的“最小化"按钮会调用此函数(系统默认)---已实现
void maxLinksGuard::changeEvent(QEvent * event )
{
if(event->WindowStateChange)
{
switch(this->windowState())
{
//点击“最小化”按钮
case Qt::WindowMinimized:
this->hide();
this->setWindowFlags(Qt::Tool);//隐藏任务栏上图标
systemTray->show();//显示托盘
systemTray->showMessage(windowTitle(),"Here is system,double click will show!");//提示
event->ignore();
break;
default:
break;
}
}
}
//托盘图标事件--------已实现
void maxLinksGuard::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
//单击托盘
case QSystemTrayIcon::Trigger:
{
if(!this->isVisible())
{
this->setWindowFlags(Qt::Window); //显示之前恢复
showNormal();
}
else
{
hide();
}
break;
}
//双击托盘---貌似不起作用
case QSystemTrayIcon::DoubleClick:
{
if(!this->isVisible())
{
this->setWindowFlags(Qt::Window); //显示之前恢复
showNormal();
}
else
{
hide();
}
break;
}
case QSystemTrayIcon::MiddleClick:
{
break;
}
default: ;
}
}
//点击窗口的“关闭”按钮触发closeEvent事件(关闭应用程序)----已实现
void maxLinksGuard::closeEvent(QCloseEvent *event)
{
if (this->isVisible())
{
QMessageBox::critical(NULL,QObject::tr("Prompt:"),QObject::tr("Are you sure you want to kill/stop the MaxLinksGuard?"));
event->ignore();
}
else
{
event->accept();
}
}
===========================================================
托盘图标,一个自己脑子出现很久的词,可惜自己都没动手去实现。最近看见的,听见的多了,自己也感兴趣就弄弄了,感觉还蛮简单了。
贴出效果图:
那么多功能,其实就一个类就搞定了,那就是QSystemTrayIcon
头文件(主要 1.声明菜单相关动作 2.声明系统托盘对象,以及相关托盘槽函数 3.关闭事件)
- #ifndef SYSTEMTRAYICON_H
- #define SYSTEMTRAYICON_H
- #include <QMainWindow>
- #include <QMenu>
- #include <QSystemTrayIcon>
- #include <QCloseEvent>
- namespace Ui {
- class SystemTrayIcon;
- }
- class SystemTrayIcon : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit SystemTrayIcon(QWidget *parent = 0);
- ~SystemTrayIcon();
- void CreatTrayMenu();
- void CreatTrayIcon();
- QSystemTrayIcon *myTrayIcon;
- QMenu *myMenu;
- QAction *miniSizeAction;
- QAction *maxSizeAction;
- QAction *restoreWinAction;
- QAction *quitAction;
- private:
- Ui::SystemTrayIcon *ui;
- public slots:
- void iconActivated(QSystemTrayIcon::ActivationReason reason);
- protected:
- void closeEvent(QCloseEvent *event);
- };
- #endif // SYSTEMTRAYICON_H
源文件(1.创建上下文菜单 2.创建系统托盘,实现相关功能)---From Qt Assistant
#include <QtGui>
Window::Window()
{
createIconGroupBox();
createMessageGroupBox();
iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
createActions();
createTrayIcon();
connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage()));
connect(showIconCheckBox, SIGNAL(toggled(bool)),
trayIcon, SLOT(setVisible(bool)));
connect(iconComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(setIcon(int)));
connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(iconGroupBox);
mainLayout->addWidget(messageGroupBox);
setLayout(mainLayout);
iconComboBox->setCurrentIndex(1);
trayIcon->show();
setWindowTitle(tr("Systray"));
resize(400, 300);
}
void Window::setVisible(bool visible)
{
minimizeAction->setEnabled(visible);
maximizeAction->setEnabled(!isMaximized());
restoreAction->setEnabled(isMaximized() || !visible);
QDialog::setVisible(visible);
}
void Window::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
QMessageBox::information(this, tr("Systray"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
hide();
event->ignore();
}
}
void Window::setIcon(int index)
{
QIcon icon = iconComboBox->itemIcon(index);
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->setToolTip(iconComboBox->itemText(index));
}
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1)
% iconComboBox->count());
break;
case QSystemTrayIcon::MiddleClick:
showMessage();
break;
default:
;
}
}
void Window::showMessage()
{
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(
typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
durationSpinBox->value() * 1000);
}
void Window::messageClicked()
{
QMessageBox::information(0, tr("Systray"),
tr("Sorry, I already gave what help I could.\n"
"Maybe you should try asking a human?"));
}
void Window::createIconGroupBox()
{
iconGroupBox = new QGroupBox(tr("Tray Icon"));
iconLabel = new QLabel("Icon:");
iconComboBox = new QComboBox;
iconComboBox->addItem(QIcon(":/images/bad.svg"), tr("Bad"));
iconComboBox->addItem(QIcon(":/images/heart.svg"), tr("Heart"));
iconComboBox->addItem(QIcon(":/images/trash.svg"), tr("Trash"));
showIconCheckBox = new QCheckBox(tr("Show icon"));
showIconCheckBox->setChecked(true);
QHBoxLayout *iconLayout = new QHBoxLayout;
iconLayout->addWidget(iconLabel);
iconLayout->addWidget(iconComboBox);
iconLayout->addStretch();
iconLayout->addWidget(showIconCheckBox);
iconGroupBox->setLayout(iconLayout);
}
void Window::createMessageGroupBox()
{
messageGroupBox = new QGroupBox(tr("Balloon Message"));
typeLabel = new QLabel(tr("Type:"));
typeComboBox = new QComboBox;
typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
typeComboBox->addItem(style()->standardIcon(
QStyle::SP_MessageBoxInformation), tr("Information"),
QSystemTrayIcon::Information);
typeComboBox->addItem(style()->standardIcon(
QStyle::SP_MessageBoxWarning), tr("Warning"),
QSystemTrayIcon::Warning);
typeComboBox->addItem(style()->standardIcon(
QStyle::SP_MessageBoxCritical), tr("Critical"),
QSystemTrayIcon::Critical);
typeComboBox->setCurrentIndex(1);
durationLabel = new QLabel(tr("Duration:"));
durationSpinBox = new QSpinBox;
durationSpinBox->setRange(5, 60);
durationSpinBox->setSuffix(" s");
durationSpinBox->setValue(15);
durationWarningLabel = new QLabel(tr("(some systems might ignore this "
"hint)"));
durationWarningLabel->setIndent(10);
titleLabel = new QLabel(tr("Title:"));
titleEdit = new QLineEdit(tr("Cannot connect to network"));
bodyLabel = new QLabel(tr("Body:"));
bodyEdit = new QTextEdit;
bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
"clue.\nClick this balloon for details."));
showMessageButton = new QPushButton(tr("Show Message"));
showMessageButton->setDefault(true);
QGridLayout *messageLayout = new QGridLayout;
messageLayout->addWidget(typeLabel, 0, 0);
messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
messageLayout->addWidget(durationLabel, 1, 0);
messageLayout->addWidget(durationSpinBox, 1, 1);
messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
messageLayout->addWidget(titleLabel, 2, 0);
messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
messageLayout->addWidget(bodyLabel, 3, 0);
messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
messageLayout->addWidget(showMessageButton, 5, 4);
messageLayout->setColumnStretch(3, 1);
messageLayout->setRowStretch(4, 1);
messageGroupBox->setLayout(messageLayout);
}
void Window::createActions()
{
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
quitAction = new QAction(tr("&Quit"), 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 = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
转:http://blog.sina.com.cn/s/blog_a6fb6cc90101dddb.html
http://blog.csdn.net/qivan/article/details/7506306
另可参考:http://www.cnblogs.com/csuftzzk/archive/2013/01/25/2877283.html
http://www.qtcn.org/bbs/simple/?t15361.html
http://blog.csdn.net/learningstone/article/details/7932363
http://hi.baidu.com/sdink/item/85fa00d09864e01ed90e449a
Qt中如何使窗口隐藏/最小化到托盘相关推荐
- pyqt5 最小化 系统托盘_Qt中如何使窗口隐藏/最小化到托盘
展开全部 功能62616964757a686964616fe78988e69d8331333361303733:窗口最小化后或者点击"hide"按钮将窗口隐藏到托盘. 通常最小化的 ...
- C++ 使窗口最大化最小化
使用ShowWindow函数可是设置窗体的形态 代码 #include<windows.h> #include<cstdio> #include<iostream> ...
- delphi 关闭时缩小到托盘_delphi 实现最小化系统托盘
1.new -->application 2.在form1中加入一个tPopMenu 命名为pm1 3.uses ShellAPI; 4.定义一个常量在 const WM_TRAYMSG = W ...
- qt 最小化到托盘linux,Qt窗口最小化到托盘,托盘菜单控制
作为Qt初学者,最近在编写窗口最小化到托盘功能代码的时候遇到了点阻碍. Qt自带的demo实现的功能太过繁琐,涉及知识点又太杂,很难理出最核心的思路.网上的文章代码也很多脱骨于这个demo,注释说明太 ...
- 【pyqt5学习】——窗口最小化至托盘、取消任务栏图标
目录 1.最小化至托盘 1)重写系统托盘类,设置托盘图标等属性 2)将具体的窗口放入托盘 2.取消任务栏界面图标 3.问题汇总 1)退出后托盘图标还是存在,没有消失 2)最小化后左下角会出现 窗口 3 ...
- java swt最小化到托盘_SWT 中实现最小化到托盘图标,并只能通过托盘的弹出菜单关闭程序...
package com.unmi; import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt. ...
- MFC窗口最小化到托盘
把程序放到托盘上的本质就是先在托盘区绘制一个图标,然后把程序隐藏不见,再对托盘的图标进行消息处理,就可以了.绘制图标以及确定图标所传送消息的函数只有一个,那就是 WINSHELLAPI BOOL W ...
- Electron无边框窗口(最小化、最大化、关闭、拖动)以及动态改变窗口大小
文章目录 一.目标原型 1. 目标 2. 原型设计 3. 原型初步实现 二.无边框窗口 1. 要点 2. 改造 三.可拖拽区 1. 要点 2. 改造 四.最小化.最大化.关闭 1. 要点 2. 改造 ...
- C# 窗口最小化到托盘及右键图标显示菜单
日常开发有时候需要实现窗口最小化到系统托盘,本文就来讲讲该如何实现winfrom最小化到系统托盘,本例子基于VS2019编写. 用C#开发winform桌面程序时,程序启动后,默认是显示在桌面而且在任 ...
最新文章
- 中国首次纳米孔测序大会:不可错过的教学专场和技术诊断
- AI伪装目标检测:让变色龙现出原形!
- iOS UI基础-4.1应用程序管理 字典转Model
- recycleview 嵌套高度问题_突破!10万荧光材料无一幸免,150年来都没有解决的问题,最近找到答案了!|史上最强荧光材料...
- java jsp 脚本 声明 表达式 简介
- bootstrap-进度条
- Visual.Assist.X 菜单汉化
- I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard-android.sh hacking
- [Swift]LeetCode1023. 驼峰式匹配 | Camelcase Matching
- 研究validation插件到现在的感受
- html5好看的注册页面设计,美观大气注册登录页面模板html源码
- 常用手持设备的use-agent头信息
- postman 一直Sending
- DataGrip for Mac破解步骤详解,期限到2099年
- 盒子模型塌陷解决办法
- html扫码支付,WEB端第三方支付接入 - 支付宝 - 扫码支付
- CAD特性窗口,快捷特性窗口等不显示图形类型问题
- jwt鉴权(react express jsonwebtoken)
- dwg文件怎么打开呢?dwg是啥呢?
- 学生信息管理系统——查询学生信息(Java+web综合)