qt托盘菜单半透明
QMenu#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
class TrayDialog: public QDialog{
public:
...
// add
void activeTray(QSystemTrayIcon::ActivationReason reason);
private:
// add
struct TrayDialogImpl;
TrayDialogImpl* impl{ nullptr };
}
TrayDialog.cpp
#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;
}
}
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明来自 张拓的博客!