Results 1 to 4 of 4

Thread: change color QPushButton Previous post methods not working Qt 5.4.1 VS 2013 Windows 7

  1. #1
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default change color QPushButton Previous post methods not working Qt 5.4.1 VS 2013 Windows 7

    I've looked at all the previous posts on how to change the color of a button that I could search and find, but they don't seem to work. I'm using Qt 5.4.1 VS 2013 and Windows 7. I'm trying to change the color of a button to green when checked. I've tried changing the color of QPalette::Button
    Qt Code:
    1. OnButtonToggled(bool bChecked)
    2. {
    3. if(bChecked)
    4. {
    5. QPalette pal = button->palette();
    6. pal.setColor(QPalette::Button, Qt::green);
    7. button->setAutoFillBackground(true);
    8. button->setPalette(pal);
    9. button->update();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    All this does is give me a green border around the button.
    Palette.jpg

    I also tried changing the brush of QPallete::Button
    Qt Code:
    1. OnButtonToggled(bool bChecked)
    2. {
    3. if(bChecked)
    4. {
    5. QPalette pal = button->palette();
    6. pal.setBrush(QPalette::Button, QBrush(Qt::green));
    7. button->setAutoFillBackground(true);
    8. button->setPalette(pal);
    9. button->update();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    but I get the same thing.

    if I change the style sheet
    Qt Code:
    1. button->setStyleSheet("background-color:green;");
    To copy to clipboard, switch view to plain text mode 
    it changes to a squared off button instead of the rounded button it used to be, which means it no longer matches all the other buttons in the window.
    StyleSheet.jpg
    To be honest, I don't understand why the palette option doesn't work. In all the previous posts I've read, that's what people did. If I remember right, I needed to change the color of a button a couple of years ago, and the palette method is how I did it. I was using Qt 4.3.1 and Windows XP at that time though.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change color QPushButton Previous post methods not working Qt 5.4.1 VS 2013 Windo

    You might be running into this (from the Qt Style Sheets Reference):

    QPushButton
    Supports the box model. Supports the :default, :flat, :checked pseudo states.
    For QPushButton with a menu, the menu indicator is styled using the ::menu-indicator subcontrol. Appearance of checkable push buttons can be customized using the : open and : closed pseudo-states.
    Warning: If you only set a background-color on a QPushButton, the background may not appear unless you set the border property to some value. This is because, by default, the QPushButton draws a native border which completely overlaps the background-color. For example,

    QPushButton { background-color: red; border: none; }

    See Customizing QPushButton for an example.
    So try setting the border as well.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change color QPushButton Previous post methods not working Qt 5.4.1 VS 2013 Windo

    My problem was I had no clue on how to set up style sheets. In the help I stumbled across "Qt Style Sheets Reference" which told me the syntax I needed and what it did, like "border-radius" to round the corners. I didn't know that border-radius existed or what it did. I didn't know what to call anything so that I could set it. I found it there. It's a great reference, you just have to find it. I didn't think finding it was all that easy.

  4. #4
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: change color QPushButton Previous post methods not working Qt 5.4.1 VS 2013 Windo

    Quote Originally Posted by TonyInSoMD View Post
    My problem was I had no clue on how to set up style sheets. In the help I stumbled across "Qt Style Sheets Reference" which told me the syntax I needed and what it did, like "border-radius" to round the corners. I didn't know that border-radius existed or what it did. I didn't know what to call anything so that I could set it. I found it there. It's a great reference, you just have to find it. I didn't think finding it was all that easy.
    BTW, you have to scroll down the page some to get to the list of properties

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 11:23
  2. Replies: 6
    Last Post: 25th May 2010, 11:42
  3. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 08:14
  4. How to change color of a QPushButton?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2008, 14:22
  5. Change background color for a QPushButton?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2007, 15:23

Tags for this Thread

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.