Using variables in qt StyleSheets

前端 未结 4 1495
無奈伤痛
無奈伤痛 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:14

    What you're trying to accomplish simply isn't possible using pure Qt style sheets.

    You can achieve a similar effect by modifying and reloading your style sheets from within your C++ code, for example:

    QString myColor = "#FFCC08";
    QString styleSheet = "QPushButton { background-color: %1;}";
    ...
    myWidget->setStyleSheet( styleSheet.arg(myColor) );
    

    Unfortunately this has several drawbacks (inability to preview in designer, changing code rather than a style sheet), but it's about as close as you can get to what you're trying to achieve with Qt.

提交回复
热议问题