Walsi (24th April 2007)
Actually, you may have to use the role QPalette::Window, to change the button color.
i'm having trouble changing the background color of the pushbutton, regardless of the chosen role. The only way i've been able to change it is by manipulating the styleSheet. Could you perhaps post some compilable example code where you alter the button's background color?
Edit: I take that back, using QPalette::Button worked like a charm.
Qt Code:
button->setPalette(palette);To copy to clipboard, switch view to plain text mode
Last edited by TheRonin; 24th April 2007 at 08:37. Reason: evreka!
That are the lines I inserted to change the butons color:
Qt Code:
#include <QPalette> QPalette greenPalette;To copy to clipboard, switch view to plain text mode
Don't ask about the name of the Button![]()
Best Regards,
It doesn't work so, I got following error at compiling the c-fle:
Qt Code:
error: base operand of '->' has non-pointer type 'QPalette'To copy to clipboard, switch view to plain text mode
I got no errors when I replace '->' with '.'
Look:
Qt Code:
QPalette greenPalette; destroyButton->setPalette(greenPalette);To copy to clipboard, switch view to plain text mode
But the Button is still gray?
Use QPalette::Window, instead of Background.
Button is still gray!
![]()
palette->setColor( QPalette::Window, Qt::green );
Regards
Walsi (24th April 2007)
"." instead of "->"
because with the "->" I got a compiling error!
Sure, I forgot you don't have a palette pointer![]()
but the button is still gray!
![]()
![]()
![]()
arrrr it is green!!!![]()
![]()
I even dont know what my mistake was!!
How can i use colors which are net predefined??
Is it possible to enter anywhere a RGB code???
Or something else?
Best Regards, and thx
Yes, you can construct a custom color with QColor( R, G, B ).
Look at QColor.
Regards
Yes, if you use a QTimer, with let's say a 200ms delay. In the timerEvent slot you just switch the color from gray to green and then back.
Regards
A few notes:
It is a good practice to get the palette from a widget, modify it, and set it back. Constructing a new palette object makes it use application's default palette which means losing any possible previous modifications to widget's palette:
Qt Code:
palette.setColor(...); widget->setPalette(palette);To copy to clipboard, switch view to plain text mode
For a button, the correct color group is QPalette::Button:
Qt Code:
button->setPalette(palette);To copy to clipboard, switch view to plain text mode
QWindowsXpStyle and QMacStyle use native theming engines which cause 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
I got a runtime exception: Segmentation fault!
Now I tried a solution which causes no error and which seems to be working.
My code is looking like that:
I generate my own color out from a RGB code (also with alpha Parameter)
Qt Code:
QColor myColor; mycolor.setRgb(0, 150, 0, 255); ... ... ... To copy to clipboard, switch view to plain text mode
It works fine!
Best Regards,...
fnmblot (25th April 2007)
Now try changing your application to a different style... Especially try the WindowsXP style.
Bookmarks