Using variables in qt StyleSheets

前端 未结 4 1539
無奈伤痛
無奈伤痛 2021-01-04 03:31

Is there any possibility of giving variable name to hex/rgb numbers in .qss file . For eh

myColor = #FFCC08
QPushButton { background-color: myColor;}
         


        
4条回答
  •  余生分开走
    2021-01-04 04:10

    Another way to accomplish this would be to use Dynamic Properties. This would let you easily assign multiple properties to an object or group of objects, sort of like assigning a css class to an object. https://wiki.qt.io/Dynamic_Properties_and_Stylesheets

    For example, in your UI file, you could add a string dynamic property "colorStyle" with a value of "myStyle1".

    Your stylesheet would look like:

    QPushButton[colorStyle='myStyle1'] {
        background-color: #FFCC08;
        ... any other style changes...
    }
    

    Any QPushButton you assign 'myStyle1' will follow the stylesheet if you set it globally.

提交回复
热议问题