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



  • 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


  • Lifetime Qt Champion

    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.


  • Moderators

    @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;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.