Important: Please read the Qt Code of Conduct -

[SOLVED] CSS change property from SLOT not working

  • Hi, I need to apply a CSS rule to my QSlider using properties but I am not able to do it in a SLOT (where I need to do it!) however I can do it in another function (called from the constructor).

    This is where I set my CSS (in the constructor):

                        "QSlider{background: transparent; width: 50px; height:20px; border-radius: 10px}"
                        "::groove:horizontal {"
                            "height: 20px;"
                        "::handle:horizontal {"
                            " background: rgb(245,246,248);"
                            " border: 1px solid rgb(220,220,220);"
                            "height: 20px;"
                            " width: 20px;"
                            " border-radius: 10px;"
                        "::add-page:horizontal {"
                            "background: rgb(212,212,212);"
                            "height: 20px;"
                            "border-top-right-radius: 10px;"
                            "border-bottom-right-radius: 10px;"
                            "background: rgb(251,165,0);"
                            "border-top-left-radius: 10px;"
                            "border-bottom-left-radius: 10px;"
                            "background: red;"
                            "border-top-left-radius: 10px;"
                            "border-bottom-left-radius: 10px;"

    and this is what I do in my slot:
    @ ui.slider1->setProperty("state", true);
    ui.slider2->setProperty("state", true);
    ui.slider3->setProperty("state", false);@

    I need to apply the "state" property in the slot but it is ignored while the other properties are applied correctly.

    Can anyone help me?

    Thanks a lot

  • Solved doing a first setProperty in the other method then it works in the slot!

  • Lifetime Qt Champion


    Since it's a static style sheet, why don't you apply it directly in the constructor ?

  • bq. This is where I set my CSS (in the constructor):
    bq. this->setStyleSheet("QWidget{background:rgba(246,246,246,246);}"
    bq. ......

    As I said, I set the style sheet in the constructor while at runtime I set the correct "state" property. It works but I need to set the property twice in order to see it applied like:
    @ ui.slider1->setProperty("state", true);
    ui.slider1->setProperty("state", true);


  • Lifetime Qt Champion

    Sorry, I missed the parenthesis about the constructor.

    Do you initialize the state property ?

  • How should I initialize it?

  • Lifetime Qt Champion

    That's up to you know what is a sensible default value. Probably false ?

  • Oh sure! I didn't understand what you meant!
    Yes I am initializing it in an init() method called by the constructor!

    @ui.slider1->setProperty("state", false);@

  • Lifetime Qt Champion

    I thought I was forgetting something… "Here": you have the technique

  • Well, I solved the problem. I was using that post to learn how to do it but I wasn't doing this:

    because when I started this thread I tried that piece of code but I had many errors (I don't remember which ones) and I thought it was for older versions of QT :(

    Thanks :D

  • Lifetime Qt Champion

    You're welcome !

    Since you have your stilling in place now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply