当前位置: 首页 > article >正文

QT 范例阅读:系统托盘 The System Tray Icon example

main.cpp

    QApplication app(argc, argv);
    
    //判断系统是否支持 系统托盘功能
    if (!QSystemTrayIcon::isSystemTrayAvailable()) {
        QMessageBox::critical(0, QObject::tr("Systray"),
                              QObject::tr("I couldn't detect any system tray "
                                          "on this system."));
        return 1;
    }
    //关闭 隐式退出在最后一个窗口关闭时(这个示例该行语句注释也没区别)
    QApplication::setQuitOnLastWindowClosed(false);

windows.h

protected:
    //重载关闭事件
    void closeEvent(QCloseEvent *event) override;

private slots:
    //点击托盘图标时的处理
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
    //显示托盘弹出消息
    void showMessage();
    //点击托盘消息后的处理
    void messageClicked();

private:   
    //托盘图标右键 菜单动作
    QAction *minimizeAction;
    QAction *maximizeAction;
    QAction *restoreAction;
    QAction *quitAction;

    //系统托盘
    QSystemTrayIcon *trayIcon;
    //托盘菜单
    QMenu *trayIconMenu;
void Window::createActions()
{
    minimizeAction = new QAction(tr("Mi&nimize"), this);
    connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);

    maximizeAction = new QAction(tr("Ma&ximize"), this);
    connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);

    restoreAction = new QAction(tr("&Restore"), this);
    connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
}

void Window::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(maximizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
}
    
    //是否显示托盘图标
    connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
    //托盘消息点击事件    
    connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
    //托盘图标点击事件
    connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);

void Window::closeEvent(QCloseEvent *event)
{
    //如果显示了托盘,关闭事件忽略,窗体隐藏
    if (trayIcon->isVisible()) {
        QMessageBox::information(this, tr("Systray"),
                                 tr("The program will keep running in the "
                                    "system tray. To terminate the program, "
                                    "choose <b>Quit</b> in the context menu "
                                    "of the system tray entry."));
        hide();
        event->ignore();
    }
}

//托盘图标点击消息处理
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick: //改变图标
        iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
        break;
    case QSystemTrayIcon::MiddleClick:
        showMessage();
        break;
    default:
        ;
    }
}

//显示消息
void Window::showMessage()
{
    showIconCheckBox->setChecked(true);
    QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(
            typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
    if (msgIcon == QSystemTrayIcon::NoIcon) {
        QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
        trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
                          durationSpinBox->value() * 1000);
    } else {
        trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon,
                          durationSpinBox->value() * 1000);
    }
}


http://www.kler.cn/a/227918.html

相关文章:

  • LMI Gocator GO_SDK VS2019引用配置
  • Linux二进制部署K8s集群的平滑升级教程
  • Ubuntu安装VMware17
  • hdfs:介绍三个脚本
  • HarmonyOS应用开发快速入门
  • qt内部的特殊技巧【QT】
  • 游戏如何选择服务器
  • 利用自定义注解和反射优雅处理对象字段
  • VUE3语法--computed计算属性中get和set使用案例
  • 常见的web前端开发框架介绍
  • 消息中间件(MQ)对比:RabbitMQ、Kafka、ActiveMQ 和 RocketMQ
  • 谷粒商城-P19
  • VitePress-08-文档中代码组的使用
  • sklearn 计算 tfidf 得到每个词分数
  • MySQL创建索引的注意事项
  • 华为数通方向HCIP-DataCom H12-831题库(简答题01-27)
  • pdmodel从动态模型转成静态onnx
  • WiFi测试的核心思路和主要工具
  • BAPI_PRODORD_CREATE-创建生产订单BAPI测试
  • WPS Office18.7软件日常更新
  • 代码随想录算法训练营Day48|198.打家劫舍、213.打家劫舍II、337.打家劫舍 III
  • 通过 ChatGPT 的 Function Call 查询数据库
  • 20240202在WIN10下使用fast whisper缺少cudnn_ops_infer64_8.dll
  • python+PyQt5 左右声道测试
  • Qt应用软件【协议篇】http协议get、post示例
  • 网络原理TCP/IP(1)