• 【转载】 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();
    }
    
    
    
  • 相关阅读:
    949. Largest Time for Given Digits
    450. Delete Node in a BST
    983. Minimum Cost For Tickets
    16. 3Sum Closest java solutions
    73. Set Matrix Zeroes java solutions
    347. Top K Frequent Elements java solutions
    215. Kth Largest Element in an Array java solutions
    75. Sort Colors java solutions
    38. Count and Say java solutions
    371. Sum of Two Integers java solutions
  • 原文地址:https://www.cnblogs.com/xiaqiuchu/p/16720969.html
Copyright © 2020-2023  润新知