Qt之系统托盘(QSystemTrayIcon详解)
阅读原文时间:2021年04月23日阅读:1

托盘是什么?这个就不用在多说了!而Qt中如何实现自己的托盘功能,Qt自带的demo也很明了,这里我们就来实现自己的一个托盘。。。

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()));
 }

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了!

               

更多参考:

注:

技术在于交流、沟通,转载请注明出处并保持作品的完整性。

作者:╰☆奋斗ing❤孩子`  原文:http://blog.sina.com.cn/s/blog_a6fb6cc90101dddb.html

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章