Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Using variables in style sheets qss
QtWS25 Last Chance

[Solved] Using variables in style sheets qss

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 19.6k Views
  • 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.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    Is there anyway that we can define a hex/rgb numbers to a variable and use them in our stylesheet(.qss) file . For eg

    @myColor = #DFDFE0
    QPushButton { background-color: myColor; }@

    This is required as there are many components in .qss file where i need to use the same hex value for color. So I was thinking that at the top of the stylesheet file i can define all the variable and use them as required. Also if i need to change the color then i have to change at one place and that will be reflected for all the components.

    Thanks

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pritamghanghas
      wrote on last edited by
      #2

      I dont think stylesheets have support for that.
      you can group rules together, not sure about the exact syntax

      QPushButton QLabel QWidget { background-color: #DFDFE0 }

      1 Reply Last reply
      0
      • M Offline
        M Offline
        miroslav
        wrote on last edited by
        #3

        You can use a text template for the style sheet, create the actual stylesheet as a combination of the template and a theme and set it. See Grantlee for string templating. I did something similar, used just basic string replacement and it works fine.

        Mirko Boehm | mirko@kde.org | KDE e.V.
        FSFE Fellow
        Qt Certified Specialist

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          @miroslav

          Can you give some examples about how can i use this in stylesheets(.qss) file , that will be really helpful. I have gone through the "documentation":http://www.grantlee.org/apidox/for_themers.html but couldn't understand much about how to use it.

          Thanks for your time.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stephen
            wrote on last edited by
            #5

            I'd suggest looking at the book example or the unit tests in the grantlee repository. I think the books example is the most complete small example.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              miroslav
              wrote on last edited by
              #6

              [quote author="stephen" date="1338905990"]I'd suggest looking at the book example or the unit tests in the grantlee repository. I think the books example is the most complete small example.[/quote]

              Thanks, Stephen!

              Mirko Boehm | mirko@kde.org | KDE e.V.
              FSFE Fellow
              Qt Certified Specialist

              1 Reply Last reply
              0
              • C Offline
                C Offline
                clepto
                wrote on last edited by
                #7

                can you post i little code example using it in a qt application because i need it to

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  I am trying to run the book example provided in "Grantlee repository":http://gitorious.org/grantlee.
                  Dont know where i am going wrong, whenever i run the project i get the following errors

                  @13:52:01: Running build steps for project books...
                  13:52:01: Configuration unchanged, skipping qmake step.
                  13:52:01: Starting: "C:\QtSDK\QtCreator\bin\jom.exe"
                  c:\qtsdk\desktop\qt\4.8.0\msvc2010\bin\qmake.exe -spec ......\QtSDK\Desktop\Qt\4.8.0\msvc2010\mkspecs\win32-msvc2010 CONFIG+=declarative_debug -o Makefile ..\books\books.pro
                  C:\QtSDK\QtCreator\bin\jom.exe -nologo -j 4 -f Makefile.Debug

                  jom 1.0.6 - empower your cores

                  Error: dependent 'debug\bookwindow.moc' does not exist.
                  command failed with exit code 2
                  13:52:02: The process "C:\QtSDK\QtCreator\bin\jom.exe" exited with code 2.
                  Error while building project books (target: Desktop)
                  When executing build step 'Make'@

                  What steps should i follow to make this working.
                  Thanks.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sam
                    wrote on last edited by
                    #9

                    I tried but still couldn't get this working.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #10

                      You do not need a full-blown template library just to replace some text. If this fits your needs doing a simple QString::replace() does the trick as well.

                      @
                      QPushButton { background-color: myColor; }

                      QString styleSheet = ...;
                      styleSheet.replace("myColor", "#DFDFE0");
                      styleSheet.replace(...);
                      @

                      See also "this":http://qt-project.org/forums/viewthread/18686/ thread.

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        [quote author="pritamghanghas" date="1338889298"]I dont think stylesheets have support for that.
                        you can group rules together, not sure about the exact syntax

                        QPushButton QLabel QWidget { background-color: #DFDFE0 }[/quote]
                        I just want to mention that this does not do what you expect it to do.

                        If you want to group several selectors you will have to use commas. <code>QPushButton, QLabel, QWidget { background-color: #DFDFE0 }</code>

                        Without commas, you are doing a descendant selection.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sam
                          wrote on last edited by
                          #12

                          Thanks for the help,

                          For the implementation we created a parser that would parse the css file with the user defined variable and replace the variable with the actual color code.

                          Regards
                          Soumitra.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved