Results 1 to 4 of 4

Thread: QPushButton color when clicked

  1. #1
    Join Date
    Jul 2007
    Location
    California, USA
    Posts
    62
    Thanks
    17
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QPushButton color when clicked

    I have a push button that I want to always look red even when I have done setChecked(true) to keep it depressed.

    I have this:

    Qt Code:
    1. m_fourButton->setStyleSheet(QString("QPushButton {background-color: red;} QPushButton:checked{background-color: red;} QPushButton:pressed {background-color: red;}"));
    To copy to clipboard, switch view to plain text mode 
    I also tried background:red.

    I started with QPalellete which gives me the same results.
    Qt Code:
    1. m_redPalette.setColor(QPalette::Button, Qt::red);
    2. QBrush brush;
    3. brush.setColor(Qt::red);
    4. m_redPalette.setBrush(QPalette::Window, brush);
    To copy to clipboard, switch view to plain text mode 
    and tried everything that Palette would let me for Color Roles.

    The result is that it is red when it is not checked but only the border is red when it is checked (depressed). Is there anything else I can do to make the pressed button red?

    I've attached a zip file with an image of the buttons (it wouldn't let me upload my .bmp file).

    Thanks
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton color when clicked

    I can't reproduce your problem. If I use this code:

    Qt Code:
    1. button = new QPushButton(tr("Red Button"));
    2. button->setCheckable(true);
    3. button->setStyleSheet(QString("QPushButton {background-color: red;}"));
    To copy to clipboard, switch view to plain text mode 

    I get red a button also red when depressed. Your setStyleSheet code also works without a problem. I just use the default GUI style, if you use a custom GUI style maybe you should look for the problem there.

  3. #3
    Join Date
    Jul 2007
    Location
    California, USA
    Posts
    62
    Thanks
    17
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton color when clicked

    That's really strange because I created a new project and tried it again and I still have it. What version are you using? I am using 4.3.1 on windows.

    I am not explicitly setting any kind of GUI style.

    I tried both with a designer made button and in-code button:

    Qt Code:
    1. ui.pushButton->setCheckable(true);
    2. ui.pushButton->setStyleSheet(QString("QPushButton {background-color: red;} QPushButton:checked{background-color: red;} QPushButton:pressed {background-color: red;}"));
    3.  
    4. QPushButton* button = new QPushButton(tr("Red Button"), this);
    5. button->setCheckable(true);
    6. button->setStyleSheet(QString("QPushButton {background-color: red;} QPushButton:checked{background-color: red;} QPushButton:pressed {background-color: red;}"));
    To copy to clipboard, switch view to plain text mode 

    Thanks for looking at it.

  4. #4
    Join Date
    Jul 2007
    Location
    California, USA
    Posts
    62
    Thanks
    17
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton color when clicked

    Turns out it is a regression starting with version 4.3.0 and Trolltech has now entered it into their tracking system. It works in 4.2.3.

    A side note, they say that the palette approach will not work on Windows XP because XP's theme engine, not Qt, controls the drawing of the buttons. Using style sheets is the correct approach.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.