Results 1 to 6 of 6

Thread: Change QPushButton Background Color, Qt4.3

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Change QPushButton Background Color, Qt4.3

    I am trying to change the background color of a QPushButton on a windows XP machine using Qt4.3. I have tried multiple suggestions found in the forums:
    Qt Code:
    1. QPushButton *tmpButton = (QPushButton*)sender( );
    2.  
    3. QPalette pal = tmpButton->palette( );
    4. pal.setColor( QPalette::Button, color );
    5.  
    6. tmpButton->setPalette( pal );
    7. tmpButton->setAutoFillBackground( true );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QPushButton *tmpButton = (QPushButton*)sender( );
    2.  
    3. QPalette pal = tmpButton->palette( );
    4.  
    5. pal.setColor( QPalette::Active, QPalette::Button, color );
    6. pal.setColor( QPalette::Inactive, QPalette::Button, color );
    7. tmpButton ->setPalette( pal );
    To copy to clipboard, switch view to plain text mode 

    Both instances just result in the button staying the same color, but just a thin colored boarder around the edges of the button. This same code works fine in Solaris and Linux, but not Windows. The main difference is that the top code was from Qt4.1 and the bottom code is from Qt3.3, but both still worked changed just the boarder in 4.3. Is there something I am missing in Windows?

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change QPushButton Background Color, Qt4.3

    Qt Code:
    1. pal.setColor(QPalette::Window, color);
    To copy to clipboard, switch view to plain text mode 
    Or you can give it a try with stylesheets.

    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change QPushButton Background Color, Qt4.3

    Quote Originally Posted by marcel View Post
    Qt Code:
    1. pal.setColor(QPalette::Window, color);
    To copy to clipboard, switch view to plain text mode 
    Or you can give it a try with stylesheets.

    Regards
    QPalette::Window does not change anything either. A little more explanation may be needed too, the QPushButtons that I am creating are dynamic based upon the number of colors a user wishes to set (I am making a color Theme selection for a plot), the more colors the more pushbuttons. These buttons will are set with the colors of the current theme and are capable of being modied when the QColorDialog returns a color. Will the style sheets be able to set a dynamic coloring of buttons, or just set the initial color of all buttons?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Change QPushButton Background Color, Qt4.3

    QWindowsXpStyle uses native theming engine which causes some palette modifications not to have any effect. Certain colors/brushes/whatever come from the underlying system. Style sheets is the solution to this problem. Follow the link and see the example of changing QPushButton's color there.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    Rayven (29th June 2007)

  6. #5
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Re: Change QPushButton Background Color, Qt4.3

    Quote Originally Posted by jpn View Post
    QWindowsXpStyle uses native theming engine which causes some palette modifications not to have any effect. Certain colors/brushes/whatever come from the underlying system. Style sheets is the solution to this problem. Follow the link and see the example of changing QPushButton's color there.
    That was it!! Thanks so much jpn!

  7. #6
    Join Date
    Jun 2009
    Location
    Bato, Leyte, Philippines
    Posts
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Change QPushButton Background Color, Qt4.3

    Try this. QPushButton with red background and white text color.

    m_ui->pushButton_filter->setAutoFillBackground(true);
    m_ui->pushButton_filter->setStyleSheet("background-color: rgb(255, 0, 0); color: rgb(255, 255, 255)");

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 10:23
  2. Change background color for a QPushButton?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2007, 14:23

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.