Qt Forum

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

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

    General and Desktop
    qss qrc colors stylesheet
    3
    3
    2927
    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.
    • ericg22
      ericg22 last edited by

      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

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

        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 Reply Quote 0
        • raven-worx
          raven-worx Moderators @ericg22 last edited by

          @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 Reply Quote 5
          • First post
            Last post