Qt World Summit: Submit your Presentation

Does QSS support global variable?

  • I want to create a variable. Then I can set many properties of many controls to the value of the variable. And if I need to change the value of so many properties, I just change the value of the variable instead of changing so many properties.

    Can somebody help me?


  • @Qingshui-Kong
    I assume you do not mean you are using QWidget::setStyleSheet(const QString &styleSheet) explicitly, in which case you could of course use a variable for its argument.

    Sharing attributes instead of having to repeat them is a weakness in CSS. Recently I thought I saw the following construct used by someone in QSS:

        background-color: @color1;

    but I can't find it. The @attribute would presumably allow color1 to be defined once and then re-used.

    Otherwise, I achieve it in code by setting an attribute named class to a (space-separated) list of class names, like:


    .fontSizeBigger { font-size: 14pt; }
    .backgroundColorTransparent { background-color: transparent; }


        class: fontSizeBigger backgroundColorTransparent;

    You'd have to see if you can do the latter via QML, I know nothing about that.

  • @JonB

    I don't know how you change the code. And I don't want to change code. I wonder if I can fix it by changing stylesheet.qss file only.


  • @Qingshui-Kong
    I'm not suggesting you change code for QML:

    • If you only want to alter the style sheet I think you'll have to look into that @attribute mechanism (if it exists). [EDIT Ah, this must come from https://stackoverflow.com/questions/10898399/using-variables-in-qt-stylesheets. You'll see there that QSS does not support it natively, you have to do your own pre-processing of the stylesheet to do replacements with code, which I don't think you want to do....]

    • If you want to try the class mechanism, you'll need to add something like class: fontSizeBigger backgroundColorTransparent; to your widget's QML definition, assuming QML lets you do that.

Log in to reply