【问题标题】:PyQt5 customize style of QAction in menuBarPyQt5 在 menuBar 中自定义 QAction 的样式
【发布时间】:2019-08-02 22:47:12
【问题描述】:

我发现了非常相似的问题,但没有针对此问题的合理解决方案。我想更改显示在 QMainWindow 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,因为它们不是小部件。但是,似乎可以修改背景,因为 QActions 是例如当鼠标悬停时突出显示 - 就像菜单栏中的菜单一样。有什么想法吗?

【问题讨论】:

    标签: python pyqt pyqt5 python-3.5


    【解决方案1】:

    试试看:

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

    【讨论】:

      【解决方案2】:

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

      结果:

      【讨论】:

        猜你喜欢
        • 2016-08-13
        • 2021-11-22
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        • 2020-10-19
        • 2021-07-19
        • 2015-12-19
        • 2014-11-17
        相关资源
        最近更新 更多