【转载】 Qt6.2.4 窗口最小化到托盘,托盘菜单选项,单击托盘显示窗口

参考

环境

环境 版本
windows 10
Qt 6.2.4
Qt Creator 8.0.1 (Community)
qmake

效果展示

image

代码

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QCloseEvent>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_donateToPhpBtn_clicked();
    void on_gitPageInfoBtn_clicked();

private:
    Ui::MainWindow *ui;
    QSystemTrayIcon *systemTrayIcon;
    void systemTraySet();
    void closeEvent(QCloseEvent *event);
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <QUrl>
#include <QDesktopServices>
#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QIcon>
#include <QMenu>
#include <QAction>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 设置系统托盘图标
    this->systemTraySet();
}

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


/**
 * @brief MainWindow::on_donateToPhpBtn_clicked
 * 捐赠php按钮
 */
void MainWindow::on_donateToPhpBtn_clicked()
{
    QString URL = "https://opencollective.com/phpfoundation";
    QDesktopServices::openUrl(QUrl(URL.toLatin1()));
}


/**
 * @brief MainWindow::on_gitPageInfoBtn_clicked
 * git主页
 */
void MainWindow::on_gitPageInfoBtn_clicked()
{
    QString URL = "https://gitee.com/";
    QDesktopServices::openUrl(QUrl(URL.toLatin1()));
}
/**
 * @brief MainWindow::systemTraySet
 * 创建托盘图标
 */
void MainWindow::systemTraySet()
{
    /**
     * 创建菜单对象和托盘图标对象
     * */
    systemTrayIcon = new QSystemTrayIcon(this);
    // 设置图标 首先要添加一个 qt resource文件,然后将文件添加进去,右键选择文件复制链接即可
    systemTrayIcon->setIcon(QIcon(":/images/images/icon.png"));
    // 设置托盘 单击点击事件显示窗口
    connect(systemTrayIcon ,&QSystemTrayIcon::activated, this, [=](QSystemTrayIcon::ActivationReason reason)
    {
        if(reason == QSystemTrayIcon::Trigger){
            this->show();
        }
    });
    // 设置右键菜单
    QMenu* menu = new QMenu(this);
    // 显示选项
    QAction* showBtn = new QAction( QString::fromLocal8Bit("显示"), this);
    connect(showBtn , &QAction::triggered, this, [=]()
    {
        this->show();
    });
    menu->addAction(showBtn);
    // 退出选项
    QAction* exitBtn = new QAction( QString::fromLocal8Bit("退出"), this);
    connect(exitBtn , &QAction::triggered, this, [=]()
    {
        qDebug()<<"exit";
        QApplication::exit(0);
    });
    menu->addAction(exitBtn);
    systemTrayIcon->setContextMenu(menu);
    // 显示
    systemTrayIcon->show();
}

/**
 * @brief MainWindow::closeEvent
 * @param event
 * 重写关闭窗口事件
 */
void MainWindow::closeEvent(QCloseEvent *event)
{
    this->hide();
	// 使用ignore,这样窗口就不会关闭了
    event->ignore();
}


posted @   夏秋初  阅读(432)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2019-09-22 Arduino Nano与SIM800C 通信
点击右上角即可分享
微信分享提示