簡體   English   中英

PyQt5在menuBar中自定義QAction樣式

[英]PyQt5 customize style of QAction in menuBar

我發現了非常相似的問題,但沒有針對此問題的合理解決方案。 我想更改在QMainWindow menuBar菜單中顯示的QAction的樣式/外觀(例如,更改背景顏色)。 目的是在再次瀏覽菜單時突出顯示當前選擇的動作。

例:

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例如在用鼠標懸停時會突出顯示-就像菜單欄中的菜單一樣。 有任何想法嗎?

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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM