qt QSystemTrayIcon系统托盘的使用

这里写图片描述
效果图:
下面直接上代码:
.pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2017-06-23T10:11:19
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TamiUpdateTool
TEMPLATE = app

SOURCES += main.cpp\
        dlgmain.cpp


HEADERS  += dlgmain.h


FORMS    += dlgmain.ui

RC_FILE += res.rc

RESOURCES += \
    res.qrc

win32: LIBS += -L$$PWD/lib/ -lquazipd

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

main.cpp

#include "dlgmain.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DlgMain w;
    w.show();

    return a.exec();
}

dlgmain.h

#ifndef DLGMAIN_H
#define DLGMAIN_H

#include <QDialog>
#include <QTimer>
#include <QFile>
#include <QDir>
#include <QProcess>
#include <QThread>
#include <QSettings>
#include <QProcess>
#include <QSystemTrayIcon>


namespace Ui {
class DlgMain;
}

class DlgMain : public QDialog
{
    Q_OBJECT
public:
    explicit DlgMain(QWidget *parent = 0);
    ~DlgMain();

    void init();


private slots:
    void trayicon_activated(QSystemTrayIcon::ActivationReason reason);

    void on_btn_hide_clicked();

    void on_btn_exit_clicked();

private:
    Ui::DlgMain *ui;
    bool m_bIsExtractSuccess;
    int nIndex;
    QSystemTrayIcon* m_pSystemTray;

};

#endif // DLGMAIN_H

dlgmain.cpp

#include "dlgmain.h"
#include "ui_dlgmain.h"

#include <QDebug>

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

    //去掉窗口右上角的问号
    Qt::WindowFlags flags = this->windowFlags();
    flags |= Qt::WindowMinimizeButtonHint;
    flags &= ~Qt::WindowCloseButtonHint;
    this->setWindowFlags(flags);
    this->setFixedSize(320,251);

    // 2. 托盘图标
    m_pSystemTray = new QSystemTrayIcon(this);
    m_pSystemTray->setToolTip(this->windowTitle());
    m_pSystemTray->setIcon(this->windowIcon());

    connect(m_pSystemTray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayicon_activated(QSystemTrayIcon::ActivationReason)));
    nIndex = 0;
    m_bIsExtractSuccess = false;
    init();
}

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


void DlgMain::init()
{
    ui->progressBar->setValue(0);
    ui->progressBar->hide();
    ui->label->setText(QString::fromLocal8Bit("监控升级中..."));
    m_pSystemTray->hide();//托盘图标
}//ToolTip

void DlgMain::trayicon_activated(QSystemTrayIcon::ActivationReason reason)
{
    if( reason == QSystemTrayIcon::DoubleClick || reason == QSystemTrayIcon::Context ){
        if( this->isVisible() ){
            this->hide();
            //托盘显示提示信息
            m_pSystemTray->showMessage(QString::fromLocal8Bit("Tami.RobotCore信息:"), QString::fromLocal8Bit("窗口已经隐藏..."), QSystemTrayIcon::Information, 1000);
        }else{
            this->show();
            m_pSystemTray->showMessage(QString::fromLocal8Bit("Tami.RobotCore信息:"), QString::fromLocal8Bit("窗口已经显示..."), QSystemTrayIcon::Information, 1000);
        }
    }
}

void DlgMain::on_btn_hide_clicked()
{
    m_pSystemTray->show();
    this->hide();
}

void DlgMain::on_btn_exit_clicked()
{
    this->close();
}

ui文件就你们自己画把
源码::QQ609162385

猜你喜欢

转载自blog.csdn.net/cqltbe131421/article/details/81027264