Using variables in qt StyleSheets

前端 未结 4 1530
無奈伤痛
無奈伤痛 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 03:57

    Here is a solution using sass. First, install the python bindings:

    pip install sass
    

    Then, use it:

    import sys
    import sass
    app = QApplication(sys.argv)
    
    # Create your sass style sheet (you can also write this in a file and load the file)
    style = '''
    $bg-dark: #292929;
    
    QPushButton {
    color: red;
    background-color: $bg-dark;
    }
    '''.encode('utf-8')
    
    # Compile Sass to CSS
    style = sass.compile_string(style).decode()
    
    # And set it to your app
    app.setStyleSheet(style)
    

提交回复
热议问题