如何通过整个MainWIndow将CSS样式应用于QMenu

时间:2015-01-15 23:45:54

标签: python css qt pyqt stylesheet

运行下面的示例代码会创建一个按钮窗口。点击按钮会弹出一个下拉菜单。

enter image description here

当CSS' appStyle'直接应用于menu似乎运作良好:

menu.setStyleSheet(appStyle)

但是当相同的CSS' appStyle'使用以下命令分配给整个窗口:

view.setStyleSheet(appStyle)

没有任何反应(只需注释掉menu.setStyleSheet(appStyle)即可)。 如果CSS样式表对于整个视图而言不是一次,而不是对每个菜单或每个小部件分别是一次,那将是很好的。如何实现呢?

bgColor='#1F1F1F'
appStyle="""
QMenu {{  font-size:10pt; selection-background-color: #ffaa00; selection-color: black; background-color: #7A7A7A; border-style: solid; border: 0px solid #EBEBEB; border-radius: 0; color: #EBEBEB; padding: 0px 0px 0px 0px; }}
QMenu:on  {{padding-top: 0px; padding-left: 0px; background-color: #7A7A7A; selection-background-color: #ffaa00; color: #EBEBEB; border-radius: 0;}}
QMenu QAbstractItemView  {{ border: 0px solid black; background-color: #7A7A7A; color: #EBEBEB; border-radius: 0; }}
QMenu:hover {{ border: 0px solid #ffa02f; }}
QMenu::drop-down  {{ border-radius: 0px; background-color: #7A7A7A; color: #EBEBEB; }}""".format(bgColor) 


from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__() 
        button=QToolButton(self)
        button.setText('Click Here')

        menu=QMenu()
        menu.addAction("Action01")
        menu.addAction("Action02")
        menu.addAction("Action03")

        separator=menu.addAction("")
        separator.setSeparator(True)

        subMenu=menu.addMenu('SubMenu')
        subMenu.addAction("SubAction01")
        subMenu.addAction("SubAction02")
        subMenu.addAction("SubAction03")   

        button.setMenu(menu)
        button.setPopupMode(QToolButton.InstantPopup)
        menu.setStyleSheet(appStyle)   


app=QApplication(sys.argv)    
view=Window() 
view.setStyleSheet(appStyle)
view.show()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:5)

self声明中传递视图QMenu的实例。 所以,而不是:

menu=QMenu()

做的:

menu=QMenu(self)

现在,当样式表应用于主窗口时,其样式表将沿其子项传播 - 在这种情况下为menubutton个实例。

enter image description here

工作代码如下:

bgColor='#1F1F1F'
appStyle="""
QToolButton {{border: 0px solid #0F0F0F; background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #7A7A7A, stop: 1 #0F0F0F); color: #EBEBEB}}
QMenu QAbstractItemView  {{ border: 0px solid black; background-color: #7A7A7A; color: #EBEBEB; border-radius: 0; }}
QMenu {{  font-size:10pt; selection-background-color: #ffaa00; selection-color: black; background-color: #7A7A7A; border-style: solid; border: 0px solid #EBEBEB; border-radius: 0; color: #EBEBEB; padding: 0px 0px 0px 0px; }}
QMenu:on  {{padding-top: 0px; padding-left: 0px; background-color: #7A7A7A; selection-background-color: #ffaa00; color: #EBEBEB; border-radius: 0;}}
QMenu:hover {{ border: 0px solid #ffa02f; }}
QMenu::drop-down  {{ border-radius: 0px; background-color: #7A7A7A; color: #EBEBEB; }}""".format(bgColor) 

class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__() 
        button=QToolButton(self)
        button.setText('Click Here')

        menu=QMenu(self)
        menu.addAction("Action01")
        menu.addAction("Action02")
        menu.addAction("Action03")

        separator=menu.addAction("")
        separator.setSeparator(True)

        subMenu=menu.addMenu('SubMenu')
        subMenu.addAction("SubAction01")
        subMenu.addAction("SubAction02")
        subMenu.addAction("SubAction03")   

        button.setMenu(menu)
        button.setPopupMode(QToolButton.InstantPopup)

app=QApplication(sys.argv)
view=Window() 
view.setStyleSheet(appStyle)
view.show()
sys.exit(app.exec_())