[SOLVED] Background of checked QPushButton with stylesheet



  • Hello everyone,

    The problem is simple: my checked (not pressed) QPushButton background color (setted with stylesheet) is not displayed as wanted; it looks like a Qt::Dense6Pattern is present.
    I tried modifying the stylesheet with different selectors, but no chance for now.

    Below is the simplest code.

    @
    int main(int argc, char *argv[]){
    QApplication app(argc, argv);
    QMainWindow win;
    QPushButton b1("Button 1", &win);

    b1.setCheckable(true);
    b1.setChecked(true);
    b1.setStyleSheet("QPushButton {background-color: blue;}");

    win.show();

    return app.exec();
    }
    @

    Thanks for your thoughts.


  • Moderators

    To style the "checked" state of a button you need to use
    @QPushButton:checked { ... }@
    selector.
    The thing is that default OS style for buttons don't support colored backgrounds so the control falls back to the basic windows theme (assuming you're on Windows). This theme uses pattern brush for checked buttons so that's why it's there.
    One way to workaround this is to fall back even further - into bypassing the style entirely and styling it yourself. One way to enforce that with qss is setting border manualy eg.
    @QPushButton:checked {
    background-color: blue;
    border: none; //or anything else you like
    }@



  • Modifying the border works perfectly.

    Thank you for your explainations!


Log in to reply
 

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