[SOLVED] Creating classes in Qt Stylesheet



  • Hi all!

     I'm new to Qt and I have a question regarding stylesheets. I'm using many labels in my designed and I want each label to have a different styling. As of now, I have
    

    @ QLabel
    {
    border: none;
    padding: 0;
    background: none;
    }@

    but this applies the same style to all the labels. I even tried

    @.heading
    {
    color: brown;
    }

    .heading2
    {
    color: red;
    }@

    for different styles for each label. But I can't get it to work. My question is, how can I customize the styling for each label even though I am referring to the same stylesheet?
    Thanks.



  • You can name your objects by calling QObject::setObjectName, then in the style sheet do the following

    c++
    @
    mLabel->setObjectName("Label1");
    @

    css
    @
    QLabel#Label1
    {
    color: brown;
    }
    @

    This will style all QLabels with the name of Label1.



  • That worked! Thank you, steno.



  • Hello there.
    I'd like to necro this thread a little bit because I don't think using names is a very good option; afaik they are intended to be unique... at last that's the good practice imho.

    You could always subclass and use object type (i think), but that's too much work.

    Perhaps there's a way to attribute a proper c(/q)ss 'class' to objects?

    On a off-topic note: there's a typo in the member path, for example, this works:
    http://qt-project.org/mebmer/132333
    this doesn't:
    http://qt-project.org/member/132333
    It's funny :)



  • Hello there.
    I'd like to necro this thread a little bit because I don't think using names is a very good option; afaik they are intended to be unique... at last that's the good practice imho.

    You could always subclass and use object type (i think), but that's too much work.

    Perhaps there's a way to attribute a proper c(/q)ss 'class' to objects?

    On a off-topic note: there's a typo in the member path, for example, this works:
    http://qt-project.org/mebmer/132333
    this doesn't:
    http://qt-project.org/member/132333
    It's funny :)


Log in to reply
 

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