Qt系统托盘的学习

参考:

Qt系统托盘程序的实现_qt托盘程序-CSDN博客

QT系统托盘应用程序-CSDN博客

代码:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSystemTrayIcon>
#include <QDebug>
#include <QMenu>
#include <QCloseEvent>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
public slots:
    void showWindow();
    void on_exitAppAction();
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
protected:
    void closeEvent(QCloseEvent *event);
private:
    Ui::Widget *ui;
    QSystemTrayIcon *sysTray;
    //打开工具
    //安全退出
    QAction *open;
    QAction *exit;

    QMenu *contextMenu;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    sysTray=new QSystemTrayIcon(this);
    sysTray->setIcon(QIcon(QApplication::applicationDirPath()+"/icon.ico"));
    //qDebug()<<QApplication::applicationDirPath()+"/icon.ico";

    open=new QAction("打开工具",this);
    exit=new QAction("安全退出",this);
    connect(open,&QAction::triggered,this,&Widget::showWindow);
    connect(exit,&QAction::triggered,this,&Widget::on_exitAppAction);
    connect(sysTray,&QSystemTrayIcon::activated,this,&Widget::iconActivated);
    contextMenu=new QMenu(this);
    contextMenu->addAction(open);
    contextMenu->addAction(exit);
    sysTray->setContextMenu(contextMenu);
    sysTray->show();
    this->hide();
}

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

void Widget::showWindow()
{
    show();
    raise();
}

void Widget::on_exitAppAction()
{
    qApp->exit();
}

void Widget::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
        showWindow();
        break;
    default:
        break;
    }
}

void Widget::closeEvent(QCloseEvent *event)
{
    this->hide();
    event->ignore();
}
#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

相关推荐

  1. Qt系统托盘学习

    2024-01-24 00:24:01       66 阅读
  2. QT系统托盘 及 菜单

    2024-01-24 00:24:01       33 阅读
  3. Qt学习Qt坐标系统

    2024-01-24 00:24:01       28 阅读
  4. electron录制工具-系统托盘

    2024-01-24 00:24:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 00:24:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 00:24:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 00:24:01       82 阅读
  4. Python语言-面向对象

    2024-01-24 00:24:01       91 阅读

热门阅读

  1. 第三章:交换两个变量的值

    2024-01-24 00:24:01       49 阅读
  2. LeetCode-题目整理【3】:买卖股票的最佳时机

    2024-01-24 00:24:01       49 阅读
  3. 【面试】-科大讯飞日常实习面试

    2024-01-24 00:24:01       54 阅读
  4. ImageMagick使用手册

    2024-01-24 00:24:01       64 阅读
  5. ZZULIOJ 1072: 青蛙爬井

    2024-01-24 00:24:01       57 阅读
  6. Qt —— 编写Windows截图软件(附源码)

    2024-01-24 00:24:01       66 阅读