Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using Qt private CSS parser



  • Hi dear devs.
    we have custom widget RadioButton :
    RadioButton.h

    class RadioButton : public QAbstractButton
    {
    public:
        RadioButton();
    };
    

    And a css file that contain RadioButton style properties:
    Style.css

    RadioButton {
        color:#009688;
        font-size:12pt;
        opacity:0.5;
    }
    

    Now i'm going to retrieve RadioButton's properties from Style.css
    for that after some searching i found Qt private CSS scanner and parser :
    qcssscanner.cpp
    qcssparser_p.h
    qcssparser.cpp

    And now i don't know how to use this parser to get token types + values

    int main() {
        QString css = "RadioButton {"
                        "color:#009688;"
                        "font-size:12pt;"
                        "opacity:0.5;"
                        "}";
        QCss::Parser parser(css);
    }
    

    Regards.


  • Lifetime Qt Champion

    Hi,

    You would have to enable the use of the private parts of Qt with all the big fat warnings that comes with it.

    Why do you want to parse your css since you already know what's in it ?



  • @SGaist

    You would have to enable the use of the private parts of Qt with all the big fat warnings that comes with it.

    I already compile it (and also disable warnings) :

    int main() {
        QString css = "RadioButton {"
                        "color:#009688;"
                        "font-size:12pt;"
                        "opacity:0.5;"
                        "}";
        QCss::Parser parser(css);
    }
    

    But at this time i want to get the color value (#009688) of RadioButton but the source code of this parser is complicated (may be i should make the parse tree)

    Why do you want to parse your css since you already know what's in it ?

    Because i want to read an external CSS file and get my app's widgets properties from it.
    For example : in my RadioButton painting i use an opacity value and when i change the opacity from CSS file all RadioButton must be change to new opacity.


  • Lifetime Qt Champion

    Then why not just set that external style sheet directly ?



  • @SGaist
    Well.
    We assigned stylesheet :

    class RadioButton : public QAbstractButton
    {
    public:
    	RadioButton() {
    		QString css = "RadioButton {"
    							"color:#009688;"
    							"font-size:12pt;"
    							"opacity:0.5;"
    							"}";
    		setStyleSheet(css);
    	}
    };
    

    But the problem is accessing the values So how we should get opacity :
    QPainter::setOpacity(/* required opacity */);


  • Qt Champions 2017

    @IMAN4K

    Isn't the windowOpacity property holding that?



  • @kshegunov
    No. it return 1


  • Lifetime Qt Champion

    Are you doing all the painting yourself ?



  • @SGaist
    yes


  • Lifetime Qt Champion

    What attributes are you going to support ?



  • @IMAN4K said:

    Now i'm going to retrieve RadioButton's properties from Style.css
    for that after some searching i found Qt private CSS scanner and parser :
    qcssscanner.cpp
    qcssparser_p.h
    qcssparser.cpp

    And now i don't know how to use this parser to get token types + values

    Hi.

    Something like this... Not recommended...

    #include <QtGui/5.7.0/QtGui/private/qcssparser_p.h>
    
    [...]
    QCss::Parser parser(yourStyleSheetString);
    
    while (parser.testSelector()) {
        QCss::StyleRule styleRule;
    
        if (parser.parseRuleset(&styleRule)) {
            foreach (auto sel, styleRule.selectors) {
                foreach (auto bSel, sel.basicSelectors) {
                    qDebug() << "BasicSelector:" << bSel.elementName;
                }
            }
    
            foreach (auto decl, styleRule.declarations) {
                qDebug() << "property:" << decl.d->property;
                foreach (auto value, decl.d->values) {
                   qDebug() << "value:" << value.variant;
                }
            }
        }
    }
    [...]
    


  • @Devopia53
    Excellent answer :)

    Not recommended...

    Yeah know it and i make these files local in my project (not include from <private/qcssparser_p.h>):
    qcssscanner.cpp
    qcssparser_p.h
    qcssparser.cpp

    Thank you