我在pyqt5应用程序中有一个QMenuBar,我想对此进行汇总。我只想使用id ='update'更改QAction的颜色,而使其他QAction保持不变,但是它不能按我的要求工作。这是我所拥有的:
self.update_btn = self.menu.addAction('Update')
self.update_btn.setVisible(True)
self.update_btn.setObjectName("update")
self.menu.setStyleSheet(
"""
QMenuBar > QAction#update) {
background: red;
}
""")
我尝试了其他几种方法,但没有一个起作用。
答案 0 :(得分:0)
QAction不支持styleSheet!
您可以使用QWidgetAction
尝试一下:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MenuDemo(QMainWindow):
def __init__(self,parent=None, *args):
super().__init__(parent, *args)
bar = self.menuBar()
file = bar.addMenu('File')
quit = bar.addMenu('Quit')
open_action = QAction('Open', self)
save_action = QWidgetAction(self) # <------------------------
label = QLabel(" \n New \n")
save_action.setDefaultWidget(label);
save_action.setText('New')
file.addAction(open_action)
file.addAction(save_action)
# ----- setStyleSheet ---------------
self.setStyleSheet("""
QMenu {
background-color: #ABABAB;
border: 1px solid black;
margin: 2px;
}
QMenu::item {
background-color: transparent;
padding: 20px 25px 20px 20px;
}
QMenu::item:selected {
background-color: blue;
}
QLabel {
background-color: yellow;
color: red;
font: 18px;
}
QLabel:hover {
background-color: #654321;
}
""")
quit.aboutToShow.connect(exit)
file.triggered.connect(self.selected)
self.setWindowTitle("Demo QWidgetAction")
self.resize(300, 200)
self.show()
def selected(self, q):
print('\n{} <- selected -> {}'.format(q.text(), q))
if __name__ == '__main__':
app = QApplication(sys.argv)
menus = MenuDemo()
sys.exit(app.exec_())