Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Using Qt private CSS parser

    General and Desktop
    css parser qcssparser qt5
    4
    12
    1537
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • IMAN4K
      IMAN4K last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        IMAN4K 1 Reply Last reply Reply Quote 2
        • IMAN4K
          IMAN4K @SGaist last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Then why not just set that external style sheet directly ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            IMAN4K 1 Reply Last reply Reply Quote 2
            • IMAN4K
              IMAN4K @SGaist last edited by

              @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 */);

              kshegunov 1 Reply Last reply Reply Quote 0
              • kshegunov
                kshegunov Moderators @IMAN4K last edited by

                @IMAN4K

                Isn't the windowOpacity property holding that?

                Read and abide by the Qt Code of Conduct

                IMAN4K 1 Reply Last reply Reply Quote 0
                • IMAN4K
                  IMAN4K @kshegunov last edited by IMAN4K

                  @kshegunov
                  No. it return 1

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Are you doing all the painting yourself ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    IMAN4K 1 Reply Last reply Reply Quote 1
                    • IMAN4K
                      IMAN4K @SGaist last edited by

                      @SGaist
                      yes

                      D 1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        What attributes are you going to support ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply Reply Quote 1
                        • D
                          Devopia53 @IMAN4K last edited by

                          @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;
                                      }
                                  }
                              }
                          }
                          [...]
                          
                          IMAN4K 1 Reply Last reply Reply Quote 2
                          • IMAN4K
                            IMAN4K @Devopia53 last edited by IMAN4K

                            @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

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post