Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to change button style sheet?



  • I know we can change style sheet of a button like in css but I don't know how to access the style sheet
    I want to use this: a6222350-142e-4e38-ac90-6a7957b340cf-image.png

    But I only know this: 8cadc06d-8e68-4c7a-9419-130fd41b8b8f-image.png

    I just don't know how to access to the part when you can write like in css?
    Please help me !


  • Lifetime Qt Champion

    Hi,

    If you want some sort of editable stylesheet, you should make it's content a member variable so you can then modify it to match what you want.



  • @Victor-Delattre

    What do you mean? Variable RGB colors instead of yellow? Where exactly is the problem?
    The stylesheet can be treated as a regular QString, so you can make use of QString::args.

    int value = 255;
    QString style("%1").args(value);
    

  • Lifetime Qt Champion

    Hi

    Its very unclear what you ask about , but after some coffee I realized you may be asking how to use
    same syntax as in the first screen shot

    For that you can use c++ raw strings syntax
    the R"()" part.

    This allows you to have any text directly AS IS

    
    QString Sheet = R"(
    QPushButton#pushButton {
        background-color: yellow;
    }
    QPushButton#pushButton:pressed {
        background-color: rgb(224, 0, 0);     
    }
    QPushButton#pushButton:hover {
        background-color: rgb(224, 255, 0);
    })";
    
    setStyleSheet(sheet);
    
    
    


  • @mrjj THANK YOU :))
    Helped a lot!


Log in to reply