Important: Please read the Qt Code of Conduct -

[Solved] Updating an object stylesheet to change color on "pressed" condition

  • In the constructor of my widget I have the following:
    setStyleSheet("QPushButton:pressed { background-color: #808080 }");

    Every button that's pushed turns colors. Depending on the state of each button, I change their individual stylesheets to change the normal background. I do something like:
    button1->setStyleSheet("background: qradialgradient(cx: 0, cy: 0, radius: 0.8, fx: 0.5, fy: 0.5, stop: 0 #F0FFC0, stop: 1 #00FF50)";

    The normal background changes fine, but after the first press event, it doesn't change background colors while it's being pressed. I tried all the combinations I could think of in the object setStyleSheet, but I never found one that worked. A lot of them caused a "Could not parse stylesheet of widget xxxx" error. How do I change the pressed state when setting an object stylesheet?

  • Moderators

    You mean something like this?
    @#include <QApplication>
    #include <QPushButton>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    QPushButton pb("Button");
    pb.setStyleSheet("QPushButton { background-color: red; }"
                     "QPushButton:pressed { background-color: blue; }");;
    return a.exec(&#41;;


Log in to reply