qt之系统托盘

出处:http://blog.sina.com.cn/s/blog_a6fb6cc90101dddb.html


托盘是什么?这个就不用在多说了!而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了!

           Qt之系统托盘(QSystemTrayIcon详解)     Qt之系统托盘(QSystemTrayIcon详解)          Qt之系统托盘(QSystemTrayIcon详解)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值