Important: Please read the Qt Code of Conduct -

Using CSS class

  • Hello
    Ho can I create to groups of QLabels, styled with CSS? So that all the QLabels belonging to one of the groups have similar properties, and
    the ones belonging to the other has similar as well. using #label_name I can style a single QLabel, but what should I refer to to apply the style to a whole class .labelclass{...} ? thanks

  • I'm using as well to set background of all QLabel of the my Project :

    this->setStyleSheet("QLabel "
    "background-color: rgb(255, 255, 255);"

    it's Woking 100%

  • My problem is that I have have different "groups" of labels that have different appearances. What I'm trying to achieve is something similar to

    @QLabel#firstlabel { background-color: red;}
    QLabel#secondlabel { background-color: green;} @

    This works fine for individual items were I can refer using the label name, but how can I solve the situation with 20 "firstlabels" and 20 "secondlabels" without defining the styles for each and everyone separately?

    • Richard

  • I hope you already find the solution, but because this topic is in first places in google, when you search "qt css class", I will try to give a proper answer. Don't call me a necroposter, please =)

    At first need to add "class" property to your QObject. Then set this property with some definition, in this example it is "my_supa_button".

    QVariant result = "class" );
    if( result.isValid() )
    MyLabel.setProperty( "class", "my_supa_button" );

    Then in .css file need to add css class "my_supa_button" implementation:

    *[class=my_supa_button] {
    font: 50px;
    color: black;

    So in this example every QObject can use this stylesheet, if it have QProperty "class" with value "my_supa_button".

    I hope my answer will help someone =)

  • [quote author="Kragius" date="1356006166"]
    I hope my answer will help someone =)

    Great Thanks, Kragius!

  • If you don't need to use QObject::findChild() then you can assign the same name (through setObjectName() ) to multiple items. names are not required to be unique. Then you can specify the stylesheet as you specify in your second post

Log in to reply