当前位置:编程学习 > C/C++ >>

Qt-托盘功能

1 添加两个成员变量
QSystemTrayIcon *trayicon;

QMenu *trayiconMenu;

 

 

2 添加一个槽函数用于响应在托盘图标及托盘菜单上的鼠标点击操作

void onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason);

 

 

3 在构造函数中对添加的两个变量进行初始化

 trayicon = new QSystemTrayIcon(this);

  //创建QIcon对象,参数是图标资源,值为项目的资源文件中图标的地址

  QIcon icon("F:/Windows QT/TrayIcon/bottom.png");

  trayiconMenu = new QMenu(this);

  //为托盘菜单添加菜单项

  trayiconMenu->addAction(ui->action_quit);

  //为托盘菜单添加分隔符

  trayiconMenu->addSeparator();

 

  //将创建的QIcon对象作为系统托盘图标

  trayicon->setIcon(icon);

  //显示托盘图标

  trayicon->show();

  //设置系统托盘提示

  trayicon->setToolTip(tr("托盘测试"));

  //将创建菜单作为系统托盘菜单  

  trayicon->setContextMenu(trayiconMenu);

 

  //在系统托盘显示气泡消息提示

  trayicon->showMessage(tr("hahaya"), tr("托盘测试"), QSystemTrayIcon::Information, 5000);

  //为系统托盘绑定单击信号的槽 即图标激活时

  connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason)));

 

 

4 实现槽函数onSystemTrayIconClicked

void MainWindow::onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason)

{

  switch(reason)

  {

  //单击

  case QSystemTrayIcon::Trigger:

  //双击

  case QSystemTrayIcon::DoubleClick:

      //恢复窗口显示

      this->setWindowState(Qt::WindowActive);

      this->show();

      break;

  default:

      break;

  }

}

 

 

5  当点击窗口的关闭按钮时,将程序托盘,则重写类的closeEvent函数

void MainWindow::closeEvent(QCloseEvent *event)

{

  if(trayicon->isVisible())

  {

      hide();

      event->ignore();

  }

}

 

 

6 点击“退出”菜单的槽函数,点击退出时托盘

void MainWindow::on_action_quit_triggered()

{

  this->hide();

  this->close();

}

 


//mainwindow.h  
#ifndef MAINWINDOW_H  
#define MAINWINDOW_H  
#include <QMainWindow>  
#include <QSystemTrayIcon>  
#include <QMenu>  
#include <QCloseEvent>  
namespace Ui { 
  class MainWindow; 

class MainWindow : public QMainWindow 

  Q_OBJECT 
public: 
  explicit MainWindow(QWidget *parent = 0); 
  ~MainWindow(); 
private: 
 
  Ui::MainWindow *ui; 
private slots: 
  void on_action_quit_triggered(); 
  void onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason); 
protected: 
  void closeEvent(QCloseEvent *event); 
private: 
  QSystemTrayIcon *trayicon; 
  QMenu *trayiconMenu; 
}; 
#endif // MAINWINDOW_H 
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QCloseEvent>
namespace Ui {
  class MainWindow;
}
class MainWindow : public QMainWindow
{
  Q_OBJECT
public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
private:

  Ui::MainWindow *ui;
private slots:
  void on_action_quit_triggered();
  void onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason);
protected:
  void closeEvent(QCloseEvent *event);
private:
  QSystemTrayIcon *trayicon;
  QMenu *trayiconMenu;
};
#endif // MAINWINDOW_H

 


//mainwindow.cpp  
#include <QtGui>  
#include "mainwindow.h"  
#include "ui_mainwindow.h"  
MainWindow::MainWindow(QWidget *parent) : 
  QMainWindow(parent), 
  ui(new Ui::MainWindow) 

  ui->setupUi(this); 
  trayicon = new QSystemTrayIcon(this); 
  //创建QIcon对象,参数是图标资源,值为项目的资源文件中图标的地址  
  QIcon icon("F:/Windows QT/TrayIcon/bottom.png"); 
  trayiconMenu = new QMenu(this); 
  //为托盘菜单添加菜单项  
  trayiconMenu->addAction(ui->action_quit); 
  //为托盘菜单添加分隔符  
  trayiconMenu->addSeparator(); 
  //将创建的QIcon对象作为系统托盘图标  
  trayicon->setIcon(icon); 
  //显示托盘图标  
  trayicon->show(); 
  //设置系统托盘提示  
  trayicon->setToolTip(tr("托盘测试")); 
  //将创建菜单作为系统托盘菜单  
  trayicon->setContextMenu(trayiconMenu); 
  //在系统托盘显示气泡消息提示  
  trayicon->showMessage(tr("hahaya"), tr("托盘测试"), QSystemTrayIcon::Information, 5000); 
  //为系统托盘绑定单击信号的槽 即图标激活时  
  connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason))); 

MainWindow::~MainWindow() 

  delete ui; 

void MainWindow::onSystemTrayIconClicked(QSystemTrayIcon::ActivationReason reason) 

  switch(reason) 
  { 
  //单击  
  case QSystemTrayIcon::Trigger: 
  //双击  
  case QSystemTrayIcon::DoubleClick: 
      //恢复窗口显示  
      this->setWindowState(Qt::WindowActive); 
      this->show(); 
      break; 
  default: 
      break; 
  } 

void MainWindow::closeEvent(QCloseEvent *event) 

  if(trayicon->isVisible()) 
  { 
      hide(); 
      event->ignore(); 
  } 

void MainWindow::on_action_quit_triggered() 

  this->hide(); 
  this->close(); 

//mainwindow.cpp补充:软件开发 , C语言 ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,