PyQt5自定义菜单栏中QAction的样式

PyQt5 customize style of QAction in menuBar

我发现了非常相似的问题,但没有针对此问题的合理解决方案。我想更改显示在 QMainWindow menuBar 菜单中的 QActions 的 style/appearance (例如更改背景颜色)。目的是在再次浏览菜单时突出显示当前选择的操作。

示例:

from PyQt5 import QtWidgets, QtCore

class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)

        action1 = QtWidgets.QAction("action1", self)
        action2 = QtWidgets.QAction("action2", self)
        action1.triggered.connect(self.print_stuff)
        action2.triggered.connect(self.print_stuff)

        mainMenu = self.menuBar()
        mainMenu.setNativeMenuBar(False)
        fileMenu = mainMenu.addMenu('Menu1')
        fileMenu.addAction(action1)
        fileMenu.addAction(action2)

    def print_stuff(self):
        print('whatever')

def run():
    app = QtWidgets.QApplication([])
    application = Window()

    application.show()
    app.exec_()

run()

可以更改 menuBar 中各个菜单的样式表,但我无法更改 QActions 的样式表,因为它们不是小部件。但是,似乎可以修改背景,因为 QActions 例如当鼠标悬停时突出显示 - 就像菜单栏中的菜单一样。有什么想法吗?

使用QWidgetAction:

from PyQt5 import QtWidgets, QtCore, QtGui

class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)

        mainMenu = self.menuBar()
        mainMenu.setNativeMenuBar(False)
        fileMenu = mainMenu.addMenu('Menu1')
        action1 = QtWidgets.QAction("action1", self)
        action2 = QtWidgets.QAction("action2", self)

        action3 = QtWidgets.QWidgetAction(fileMenu)
        l = QtWidgets.QLabel("action3")
        l.setStyleSheet("QLabel { background-color : red; padding: 4 4 4 4px;}")
        action3.setDefaultWidget(l)

        fileMenu.addAction(action1)
        fileMenu.addAction(action2)
        fileMenu.addAction(action3)

        action1.triggered.connect(self.print_stuff)
        action2.triggered.connect(self.print_stuff)
        action3.triggered.connect(self.print_stuff)

    def print_stuff(self):
        print('whatever')

def run():
    app = QtWidgets.QApplication([])
    application = Window()

    application.show()
    app.exec_()

run()

结果:

试一试:

from PyQt5 import QtWidgets, QtCore, QtGui

class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)

        mainMenu = self.menuBar()
        mainMenu.setNativeMenuBar(False)
        self.fileMenu = mainMenu.addMenu('Menu1')

        action1     = QtWidgets.QWidgetAction(self)             
        self.label1 = QtWidgets.QLabel("action1")
        action1.setDefaultWidget(self.label1);
        action1.setText('action1')

        action2      = QtWidgets.QWidgetAction(self)             
        self.label2  = QtWidgets.QLabel("action2")
        action2.setDefaultWidget(self.label2);
        action2.setText('action2')

        action3      = QtWidgets.QWidgetAction(self)             
        self.label3  = QtWidgets.QLabel("action3")
        action3.setDefaultWidget(self.label3);
        action3.setText('action3')

        self.fileMenu.addAction(action1)
        self.fileMenu.addAction(action2)
        self.fileMenu.addAction(action3)

        self.fileMenu.triggered.connect(self.print_stuff)        # +++

    def print_stuff(self, q):
        print('whatever->', q.text() )
        self.label1.setStyleSheet("""
            QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
            QLabel:hover { background-color: #654321;}
            """)
        self.label2.setStyleSheet("""
            QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
            QLabel:hover { background-color: #654321;}
            """)
        self.label3.setStyleSheet("""
            QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
            QLabel:hover { background-color: #654321;}
            """)

        if q.text() == 'action1':
            self.label1.setStyleSheet("""
                QLabel { background-color : red; padding: 10px 12px 10px 12px;}
                QLabel:hover { background-color: #C10000;}
                """)
        elif q.text() == 'action2':
            self.label2.setStyleSheet("""
                QLabel { background-color : red; padding: 10px 12px 10px 12px;}
                QLabel:hover { background-color: #C10000;}
                """)
        elif q.text() == 'action3':
            self.label3.setStyleSheet("""
                QLabel { background-color : red; padding: 10px 12px 10px 12px;}
                QLabel:hover { background-color: #C10000;}
                """)


qss = """
QMenuBar {
    background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                      stop:0 lightgray, stop:1 darkgray);
}
QMenuBar::item {
    spacing: 3px;           
    padding: 2px 10px;
    background-color: rgb(210,105,30);
    color: rgb(255,255,255);  
    border-radius: 5px;
}
QMenuBar::item:selected {    
    background-color: rgb(244,164,96);
}
QMenuBar::item:pressed {
    background: rgb(128,0,0);
}

QLabel { 
    background-color: #ABABAB;
    color: rgb(255,255,255);
    font: 12px;
    padding: 10px 12px 10px 12px;
} 
QLabel:hover { 
    background-color: #654321;
} 
"""

def run():
    app = QtWidgets.QApplication([])
    app.setStyleSheet(qss)                                   # <---   
    application = Window()
    application.show()
    app.exec_()

run()