Qt开发:最小化到托盘

其实qt的帮助文档里面有个例子,另外很多api在QSystemTrayIcon类里面。

首先建立Qt的gui工程,本文默认都带了ui文件,如果不带ui文件,需要在程序里手动代码写ui,所有代码都未修改main.cpp,就不贴了。

 

初级

mainwindow.h

#include 
#include 
#include 
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    QSystemTrayIcon *trayIcon;
};

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //托盘信息相关
    QIcon icon = QIcon("icon.png"); //png,ico,svg,bmp各种格式,尺寸无限制(推荐60x60),加载图标可以用相对、绝对、资源路径
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(icon);
    trayIcon->setToolTip("a trayicon example"); //提示文字
    QString title="tip title";
    QString text="this is a tray tip";
    trayIcon->show();
    trayIcon->showMessage(title,text,QSystemTrayIcon::Information,5000); //最后一个参数为提示时长,默认10000,即10s
}
MainWindow::~MainWindow()
{
    delete ui;
}

这个最简单的例子可以实现托盘图标的显示、提示信息。

 

中级

需要用到信号槽

mainwindow.h

#include 
#include 
#include 

namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    QSystemTrayIcon *trayIcon;
    void showMessage(char *msg);
private slots:
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
};

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //托盘信息相关
    QIcon icon = QIcon("icon.png");
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(icon);
    trayIcon->setToolTip("a trayicon example");
    trayIcon->show(); //必须调用,否则托盘图标不显示

    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
        trayIcon->showMessage("title","你单击了"); //后面两个默认参数
        break;
    case QSystemTrayIcon::DoubleClick:
        trayIcon->showMessage("title","你双击了");
        break;
    case QSystemTrayIcon::MiddleClick:
        trayIcon->showMessage("title","你中键了");
        break;
    default:
        break;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

 实现了托盘图标三种不同的鼠标响应

 

高级

综合实例,增加右键菜单管理窗口

mainwindow.h

#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QSystemTrayIcon *trayIcon;
    QMenu *trayIconMenu;
    //窗口管理动作
    QAction *minimizeAction;
    QAction *maximizeAction;
    QAction *restoreAction;
    QAction *quitAction;

private slots:
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
};

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //托盘初始化
    QIcon icon = QIcon("icon.png");
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(icon);
    trayIcon->setToolTip("a trayicon example");
    trayIcon->show(); //必须调用,否则托盘图标不显示

    //创建菜单项动作(以下动作只对windows有效)
    minimizeAction = new QAction("最小化~", this);
    connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); //绑定信号槽

    maximizeAction = new QAction("最大化~", this);
    connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));

    restoreAction = new QAction("还原~", this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

    quitAction = new QAction("退出~", this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); //关闭应用,qApp对应的是程序全局唯一指针

    //创建托盘菜单(必须先创建动作,后添加菜单项,还可以加入菜单项图标美化)
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(maximizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
    trayIcon->setContextMenu(trayIconMenu);


    connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));


}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
        trayIcon->showMessage("title","你单击了"); //后面两个默认参数
        break;
    case QSystemTrayIcon::DoubleClick:
        trayIcon->showMessage("title","你双击了");
        break;
    case QSystemTrayIcon::MiddleClick:
        trayIcon->showMessage("title","你中键了");
        break;
    default:
        break;
    }

}


MainWindow::~MainWindow()
{
    delete ui;
}

效果:

Qt开发:最小化到托盘_第1张图片 Qt开发:最小化到托盘_第2张图片 Qt开发:最小化到托盘_第3张图片

 

补充

点击关闭最小化到托盘,重写closeEvent事件,注意头文件

void MainWindow::closeEvent(QCloseEvent *event)
{
    if(trayIcon->isVisible())
    {
        hide(); //隐藏窗口
        event->ignore(); //忽略事件
    }
}

同理,点击隐藏最小化到托盘重写hideEvent事件

void MainWindow::hideEvent(QHideEvent *event)
{
    if(trayIcon->isVisible())
    {
        hide(); //隐藏窗口
        trayIcon->showMessage("title","隐藏到托盘图标了"); //提示用户隐藏到了托盘
        event->ignore(); //忽略事件
    }
}


以上。

你可能感兴趣的:(Qt)