PDA

View Full Version : Change QPushButton Background Color, Qt4.3



Rayven
28th June 2007, 20:02
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:


QPushButton *tmpButton = (QPushButton*)sender( );

QPalette pal = tmpButton->palette( );
pal.setColor( QPalette::Button, color );

tmpButton->setPalette( pal );
tmpButton->setAutoFillBackground( true );




QPushButton *tmpButton = (QPushButton*)sender( );

QPalette pal = tmpButton->palette( );

pal.setColor( QPalette::Active, QPalette::Button, color );
pal.setColor( QPalette::Inactive, QPalette::Button, color );
tmpButton ->setPalette( pal );


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!

marcel
28th June 2007, 20:29
pal.setColor(QPalette::Window, color);
Or you can give it a try with stylesheets.

Regards

Rayven
28th June 2007, 20:38
pal.setColor(QPalette::Window, color);
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?

jpn
28th June 2007, 21:51
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 (http://doc.trolltech.com/4.3/stylesheet.html#customizing-a-qpushbutton-using-the-box-model) is the solution to this problem. Follow the link and see the example of changing QPushButton's color there.

Rayven
29th June 2007, 03:40
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 (http://doc.trolltech.com/4.3/stylesheet.html#customizing-a-qpushbutton-using-the-box-model) 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!

genessr
4th July 2009, 07:14
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)");