首先样式需要配合两个地方一起使用,分别是:
# 背景透明
menu.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
menu.setWindowFlags(menu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
其次需要注意Item中的 padding
这个值可能需要根据不同的情形进行微调
QMenu::item {
border-radius: 4px;
/* 这个距离很麻烦需要根据菜单的长度和图标等因素微调 */
padding: 8px 48px 8px 16px;
background-color: transparent;
}
完整测试代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年12月11日
@author: Irony
@site: http://127.0.0.1 , https://github.com/892768447
@email: 892768447@qq.com
@file:
@description:
"""
from random import choice
import string
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QFont, QIcon
from PyQt5.QtWidgets import QWidget, QMenu, QApplication
__Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2019'
__Version__ = 1.0
Style = '''
QMenu {
/* 半透明效果 */
/* background-color: rgba(255, 255, 255, 230); */
background-color: white;
border: none;
border-radius: 4px;
}
#alphaMenu {
background-color: rgba(255, 255, 255, 230);
}
QMenu::item {
border-radius: 4px;
/* 这个距离很麻烦需要根据菜单的长度和图标等因素微调 */
padding: 8px 48px 8px 16px;
background-color: transparent;
}
/* 鼠标悬停和按下效果 */
QMenu::item:selected {
/* 半透明效果 */
/* background-color: rgba(232, 232, 232, 232); */
background-color: rgb(232, 232, 232);
}
#alphaMenu::item:selected {
background-color: rgba(232, 232, 232, 100);
}
/* 禁用效果 */
QMenu::item:disabled {
background-color: transparent;
}
/* 图标距离左侧距离 */
QMenu::icon {
left: 15px;
}
/* 分割线效果 */
QMenu::separator {
height: 1px;
background-color: rgb(232,236,243);
}
'''
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(400, 300)
self.initMenu()
def contextMenuEvent(self, event):
self._contextMenu.exec_(event.globalPos())
def hello(self):
QApplication.instance().aboutQt()
def getIcon(self):
# 测试模拟图标
pixmap = QPixmap(16, 16)
pixmap.fill(Qt.transparent)
painter = QPainter()
painter.begin(pixmap)
painter.setFont(QFont('Webdings', 11))
painter.drawText(0, 0, 16, 16, Qt.AlignCenter,
choice(string.ascii_letters))
painter.end()
return QIcon(pixmap)
def initMenu(self):
self._contextMenu = QMenu(self)
# 背景透明
self._contextMenu.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
self._contextMenu.setWindowFlags(
self._contextMenu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
self._contextMenu.addAction('菜单1', self.hello)
self._contextMenu.addAction('菜单菜单2', self.hello).setEnabled(False)
self._contextMenu.addAction(self.getIcon(), '菜单3', self.hello)
self._contextMenu.addSeparator()
# 二级菜单
menu2 = QMenu('菜单菜单菜单4', self._contextMenu) # 背景透明
menu2.setObjectName('alphaMenu') # 半透明演示
menu2.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
menu2.setWindowFlags(menu2.windowFlags() |
Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
menu2.addAction(self.getIcon(), '子菜单1')
menu2.addAction(self.getIcon(), '子菜单2')
menu2.addAction(self.getIcon(), '子菜单3')
self._contextMenu.addMenu(menu2)
self._contextMenu.addAction(self.getIcon(), '菜单5', self.hello)
self._contextMenu.addAction(self.getIcon(), '菜单6', self.hello)
if __name__ == '__main__':
import sys
import cgitb
cgitb.enable(1, None, 5, '')
app = QApplication(sys.argv)
app.setStyleSheet(Style)
w = Window()
w.show()
sys.exit(app.exec_())
效果图: