Qt 自定义菜单 托盘菜单

托盘菜单实现:通过QSystemTrayIcon+QMenu+QAction即可完美实现!

实现方式:createActions用于创建菜单、菜单项,translateActions用于设置文本、实现多语化,translateAccount用于设置用户空间配额。

cpp 复制代码
void TrayMenu::createActions()
{
    help_menu = new QMenu();

    //创建菜单项
    action_show = new QAction(this);
    action_quit = new QAction(this);
    action_login_home = new QAction(this);
    action_account = new QAction(this);
    action_user_space = new QAction(this);
    action_help = new QAction(this);
    action_about = new QAction(this);
    action_check_update = new QAction(this);
    action_setting = new QAction(this);

   help_menu->setIcon(QIcon(":/icon/help"));
   action_show->setIcon(QIcon(":/icon/open"));
   action_login_home->setIcon(QIcon(":/icon/home"));
   action_account->setIcon(QIcon(":/icon/user"));
   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"));

    //添加菜单项
   help_menu->addAction(action_about);
   help_menu->addAction(action_help);
   help_menu->addAction(action_check_update);
   this->addAction(action_show);
   this->addAction(action_login_home);
   this->addSeparator();
   this->addAction(action_account);
   this->addAction(action_user_space);
   this->addSeparator();
   this->addAction(action_setting);
   this->addMenu(help_menu);
   this->addAction(action_quit);

    //设置信号连接
    connect(action_show, SIGNAL(triggered(bool)), this, SIGNAL(showWidget()));
    connect(action_quit, SIGNAL(triggered(bool)), this, SIGNAL(logoutWidget()));
    connect(action_setting, SIGNAL(triggered(bool)), this, SIGNAL(setUp()));
    connect(action_about, SIGNAL(triggered(bool)), this, SIGNAL(aboutUs()));
    connect(action_login_home, SIGNAL(triggered(bool)), MenuAction::getInstance(), SLOT(openLoginHome()));
    connect(action_help, SIGNAL(triggered(bool)), MenuAction::getInstance(), SLOT(openHelpMe()));
    QObject::connect(action_check_update, SIGNAL(triggered(bool)), MenuAction::getInstance(), SLOT(openCheckUpdate()));
}

void TrayMenu::translateActions()
{
   help_menu->setTitle(tr("help"));
   action_show->setText(tr("open"));
   action_quit->setText(tr("quit"));
   action_login_home->setText(tr("login home"));
   this->translateAccount();
   action_help->setText(tr("instruction"));
   action_about->setText(tr("about us"));
   action_check_update->setText(tr("check update"));
   action_setting->setText(tr("setting"));
}

void TrayMenu::translateAccount()
{
   action_user_space->setText(tr("use:") + use_space + QString("  ") + tr("total:") + total_space);
}

   托盘菜单项建立完成之后,在建立自己的托盘,包括:托盘图标、托盘提示信息等。
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("托盘显示内容"));
相关推荐
qq_441996056 分钟前
Mybatis官方生成器使用示例
java·mybatis
Qter_Sean7 分钟前
自己动手写Qt Creator插件
开发语言·qt
何曾参静谧12 分钟前
「QT」文件类 之 QIODevice 输入输出设备类
开发语言·qt
巨大八爪鱼12 分钟前
XP系统下用mod_jk 1.2.40整合apache2.2.16和tomcat 6.0.29,让apache可以同时访问php和jsp页面
java·tomcat·apache·mod_jk
PGCCC13 分钟前
【PGCCC】Postgresql 存储设计
数据库·postgresql
码上一元2 小时前
SpringBoot自动装配原理解析
java·spring boot·后端
计算机-秋大田2 小时前
基于微信小程序的养老院管理系统的设计与实现,LW+源码+讲解
java·spring boot·微信小程序·小程序·vue
PcVue China2 小时前
PcVue + SQL Grid : 释放数据的无限潜力
大数据·服务器·数据库·sql·科技·安全·oracle
魔道不误砍柴功4 小时前
简单叙述 Spring Boot 启动过程
java·数据库·spring boot
失落的香蕉4 小时前
C语言串讲-2之指针和结构体
java·c语言·开发语言