一.设置程序图标
1.在创建项目时注意选择
它会自动生成一个*.ico文件,若不想用,直接替换掉即可。
2.若忘记选择这一步,则可以自己手动创建一个*.rc文件,内容参考例子,添加到项目里,效果一样。
例如:IDI_ICON1 ICON DISCARDABLE “child.ico"
3.也可在程序里使用setWindowIcon()来添加
例如:
QIcon icon("D:\\Qt\\project(creator)\\serial\\1.jpg")
w.setWindowIcon(icon)
二.设置系统托盘图标
.h文件:
例如:QStstemIcon m_sysTray
.cpp文件:
m_sysTray.setToolTip("")//设置提示文字
m_sysTray.setIcon(QIcon("路径"))//设置图标
QMenu* menu = new QMenu()//托盘菜单
menu->addAction(ui.actionExit)//可添加托盘右键菜单
m_sysTray.setContextMenu(menu)//设置右键菜单
m_sysTray.show()//显示
.h 文件protected slots:
void onMenuTriggered(QAction*)12
.cpp 文件
// 菜单
QMenu *pMenu = new QMenu(this)
pMenu->addAction(QString::fromLocal8Bit("我"))
pMenu->addSeparator()
pMenu->addAction(QString::fromLocal8Bit("爱"))
QMenu *pChildMenu = new QMenu(this)
pChildMenu->setTitle(QStringLiteral("学"))
pChildMenu->addAction(QStringLiteral("Q"))
pMenu->addMenu(pChildMenu)
QAction *act1 = new QAction(pChildMenu)
act1->setText(QStringLiteral("t"))
act1->setIcon(QIcon("src/Me.png"))
pChildMenu->addAction(act1)
QPushButton *pButton = new QPushButton(this)
// 按钮
pButton->setText(QString::fromLocal8Bit("主菜单"))
// 设置菜单
pButton->setMenu(pMenu)
connect(pMenu, SIGNAL(triggered(QAction*)), this, SLOT(onMenuTriggered(QAction*)))123456789101112131415161718192021222324
void MyClass::onMenuTriggered(QAction *action)
{
QString str = action->text()
}
功能:窗口最小化后或者点击“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()
}
}
==========================================================
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)