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. How to store colors and fonts as qss or qrc properties?
QtWS25 Last Chance

How to store colors and fonts as qss or qrc properties?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qssqrccolorsstylesheet
3 Posts 3 Posters 3.8k 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.
  • E Offline
    E Offline
    ericg22
    wrote on 26 May 2016, 21:38 last edited by
    #1

    Hi Qt'ers,

    Can anyone tell me how to store and programatically access styles that are defined in a qss or qrc file using Qt 5.5? I am trying to replicate the ability provided by the Android SDK which allows properties such as colors, fonts and margins to be symbolically defined in an XML file and the utilized by both the layout tool and via an API. I thought that I would able to define say a color in either my qss file or as a resource in a .qrc file but I haven't been able to make either work. Any pointers on how to do this are greatly appreciated.

    Regards,

    Eric Gilbertson
    Peloton Technology

    R 1 Reply Last reply 27 May 2016, 05:55
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 May 2016, 23:05 last edited by
      #2

      Hi,

      Are you using that for a Widget based application ?

      A .qss file is just a text file so you can parse it for the values you are looking for or you can just make separated files that you apply as you want.

      .qrc has nothing to do with it except embedding the .qss files in your application binary.

      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
      0
      • E ericg22
        26 May 2016, 21:38

        Hi Qt'ers,

        Can anyone tell me how to store and programatically access styles that are defined in a qss or qrc file using Qt 5.5? I am trying to replicate the ability provided by the Android SDK which allows properties such as colors, fonts and margins to be symbolically defined in an XML file and the utilized by both the layout tool and via an API. I thought that I would able to define say a color in either my qss file or as a resource in a .qrc file but I haven't been able to make either work. Any pointers on how to do this are greatly appreciated.

        Regards,

        Eric Gilbertson
        Peloton Technology

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 27 May 2016, 05:55 last edited by
        #3

        @ericg22
        You can simply set properties of widgets via QSS. Most basic types are parsed. Even enums can be used by name when registered to the meta system.

        class MyWidget : public QWidget
        {
             Q_OBJECT
             Q_PROPERTY( QColor CustomColor READ ... WRITE ... DESIGNABLE true )
             Q_RPOPERTY( QFont CustomFont READ ... WRITE ... DESIGNABLE true )
             Q_PROPERTY( QString CustomString READ ... WRITE ... DESIGNABLE true )
             Q_PROPERTY( int CustomInteger READ ... WRITE ... DESIGNABLE true )
             Q_PROPERTY( QIcon CustomIcon READ ... WRITE ... DESIGNABLE true )
             Q_PROPERTY( MyEnum CustomEnum READ ... WRITE ... DESIGNABLE true )
             Q_ENUMS( MyEnum )
        ...
             enum MyEnum { 
                 MyEnumValue1,
                 MyEnumValue2
             }
        }
        

        example qss to set properties:

        MyWidget {
            qpropert-CustomColor: rgba(...); //also rgb(), hsv(), rgba(), named-colors, #XXXXXX, etc. are possible9
            qproperty-CustomFont: "serif,-1,14,5,0,0,0,0,0,0"; // see QFont::fromString(): Font-Family, pointSizeF, pixelSize, QFont::StyleHint, QFont::Weight, QFont::STyle, underline, strikeout, fixedPitch, rawMode
            qproperty-CustomFont: "Times New Roman,12"; //should also be possible
            qproperty-CustomString: "Any string...";
            qproperty-CustomInt: 8253;
            qproperty-CustomIcon: url(:/path/to/qrc/resource/icon.png);
            qproperty-CustomEnum: MyEnumValue2;
        }
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        5

        2/3

        26 May 2016, 23:05

        • Login

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