PyQt5在menuBar中自定义QAction的样式

问题描述 投票:0回答:2

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

例:

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中单个菜单的StyleSheet但我不能更改QActions的StyleSheet,因为它们不是小部件。但是,似乎可以修改背景,因为QAction是例如当它们被鼠标悬停时突出显示 - 就像菜单栏中的菜单一样。有任何想法吗?

python pyqt pyqt5 python-3.5
2个回答
0
投票

使用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()

结果:

result


0
投票

试试吧:

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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.