Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Background of checked QPushButton with stylesheet
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Background of checked QPushButton with stylesheet

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 7.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Olyver
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      }@

      1 Reply Last reply
      0
      • O Offline
        O Offline
        Olyver
        wrote on last edited by
        #3

        Modifying the border works perfectly.

        Thank you for your explainations!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved