qt托盘菜单半透明1234567891011121314151617181920212223242526272829303132QMenu#toptoolbarmenu { padding: 10px 0; border-radius: 15px; background-color: rgba(38, 40, 42, 0.7);}QMenu#toptoolbarmenu::separator { height: 2px; margin: 6px 0; background-color: rgba(255, 255, 255, 1);}QMenu#toptoolbarmenu::icon { padding: 0 0 0 10px;}QMenu#toptoolbarmenu::item { color: white; padding: 3px 10px; font-weight: bold; background-color: transparent;}QMenu#toptoolbarmenu::item:hover,QMenu#toptoolbarmenu::item:pressed,QMenu#toptoolbarmenu::item:selected { color: rgb(95, 174, 200); border-radius: 15px;}QMenu#toptoolbarmenu::item QLabel { color: rgb(145, 145, 145); font-weight: bold; padding-left: 7px; margin-bottom: 5px; background-color: transparent;} 对话框构造函数TrayDialog.h 123456789101112class TrayDialog: public QDialog{public: ... // add void activeTray(QSystemTrayIcon::ActivationReason reason);private: // add struct TrayDialogImpl; TrayDialogImpl* impl{ nullptr };} TrayDialog.cpp 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include "traydialog.h"#include <QMenu>#include <QList>#include <QAction>#include <QObject>#include <QSystemTrayIcon>#include <functional>#include <QDebug>struct TrayDialog::TrayDialogImpl { // 托盘 Menu* trayMenu{ nullptr }; QSystemTrayIcon* systemTray{ nullptr }; TrayDialogImpl() { }};TrayDialog::TrayDialog(){ ... // add impl->trayMenu = new Menu(this); impl->systemTray = new QSystemTrayIcon(this); // menu impl->trayMenu->setObjectName("toptoolbarmenu"); impl->trayMenu->setProperty("class", "toptoolbarmenu"); impl->trayMenu->setWindowFlags(impl->trayMenu->windowFlags() | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint); impl->trayMenu->setAttribute(Qt::WA_TranslucentBackground); // tray impl->systemTray->setIcon(QIcon("icon.png")); impl->systemTray->setToolTip("TrayDialog"); impl->systemTray->setContextMenu(impl->trayMenu); impl->systemTray->show(); // connect connect(impl->systemTray, &QSystemTrayIcon::activated, this, &TrayDialog::activeTray);//点击托盘,执行相应的动作 QAction* action = new QAction(QIcon(""), "123"); QObject::connect(action, &QAction::triggered, []() { qDebug() << "123"; }); QAction* show = new QAction(QIcon(""), "show"); QObject::connect(show, &QAction::triggered, [&]() { this->show(); }); impl->trayMenu->addAction(show); impl->trayMenu->addAction(action);}void TrayDialog::activeTray(QSystemTrayIcon::ActivationReason reason){ switch (reason) { case QSystemTrayIcon::Context: // 已请求系统托盘条目的上下文菜单 { ; } break; case QSystemTrayIcon::DoubleClick: // 双击 { ; } break; case QSystemTrayIcon::Trigger: // 单击 { ; } break; }}