PDA

View Full Version : Colored Buttons



Walsi
24th April 2007, 07:27
How can I give a Button another BackgroundColor??

I tried it with:


myButton->setPalette(QPalette(QColor(Qt::green)));

But it doesn't work; I got following error at compiling the c-file:


error: expected primary-expression before '(' token.

marcel
24th April 2007, 07:35
QPalette palette;
palette->setBrush( QPalette::Background, QBrush( Qt::green ) );
myButton->setPalette( palette );


Check out the color roles in QPalette.

Regards

marcel
24th April 2007, 07:47
Actually, you may have to use the role QPalette::Window, to change the button color.

Walsi
24th April 2007, 07:50
Actually, you may have to use the role QPalette::Window, to change the button color.

Please explain more detailed!

Walsi
24th April 2007, 07:57
QPalette palette;
palette->setBrush( QPalette::Background, QBrush( Qt::green ) );
myButton->setPalette( palette );


Check out the color roles in QPalette.

Regards

It doesn't work so, I got following error at compiling the c-fle:


error: base operand of '->' has non-pointer type 'QPalette'

I got no errors when I replace '->' with '.'

Look:


QPalette greenPalette;
greenPalette.setBrush(QPalette::Background, QBrush(Qt::green));
destroyButton->setPalette(greenPalette);

But the Button is still gray?

marcel
24th April 2007, 08:01
Use QPalette::Window, instead of Background.

Walsi
24th April 2007, 08:11
Button is still gray!

:confused:

marcel
24th April 2007, 08:14
palette->setColor( QPalette::Window, Qt::green );

Regards

Walsi
24th April 2007, 08:17
"." instead of "->"

because with the "->" I got a compiling error!

marcel
24th April 2007, 08:21
Sure, I forgot you don't have a palette pointer :)

Walsi
24th April 2007, 08:25
but the button is still gray!

:confused: :confused: :confused:

Walsi
24th April 2007, 08:30
arrrr it is green!!! :D :D

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

TheRonin
24th April 2007, 08:32
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.


QPalette palette = button->palette();
palette.setColor(QPalette::Button, Qt::red);
button->setPalette(palette);

marcel
24th April 2007, 08:33
Yes, you can construct a custom color with QColor( R, G, B ).
Look at QColor.

Regards

Walsi
24th April 2007, 08:36
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?


That are the lines I inserted to change the butons color:



#include <QPalette>
QPalette greenPalette;
greenPalette.setColor(QPalette::Window, Qt::green);
destroyButton->setPalette(QPalette(QColor(Qt::green)));


Don't ask about the name of the Button :cool:

Best Regards,

Walsi
24th April 2007, 08:38
Yes, you can construct a custom color with QColor( R, G, B ).
Look at QColor.

Regards

Another basic question from me: Is a QPushButton able to blink??

For example: gray --> green --> gray --> green

Best Regards!

marcel
24th April 2007, 08:40
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

jpn
24th April 2007, 08:41
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:


QPalette palette = widget->palette();
palette.setColor(...);
widget->setPalette(palette);


For a button, the correct color group is QPalette::Button:


QPalette palette = button->palette();
palette.setColor(QPalette::Button, Qt::green);
button->setPalette(palette);


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 (http://doc.trolltech.com/4.2/stylesheet.html) is the solution to this problem. Follow the link and see the example of changing QPushButton's color there.

Walsi
24th April 2007, 08:51
I got a runtime exception: Segmentation fault!

Walsi
24th April 2007, 09:31
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)



QColor myColor;
mycolor.setRgb(0, 150, 0, 255);

...
...
...

myButton->setPalette(QPalette(Qcolor(myColor)));


It works fine!

Best Regards,...

Walsi
24th April 2007, 10:32
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

How can I find out which gray is the standard gray!!!! So that the Button looks like a button with no specific color??



Best Regards?

wysota
24th April 2007, 10:33
Now try changing your application to a different style... Especially try the WindowsXP style.

Walsi
24th April 2007, 11:55
give me a hint, please! :cool:

TheRonin
24th April 2007, 13:45
give me a hint, please! :cool:


QColor oldButtonColor = pushButton->palette().color(QPalette::Button);

aamer4yu
25th April 2007, 06:40
Walsi, which OS were you using ??
I was using the same code to change the color of the button...
setPalette(QPalette(QColor(m_color)));
but i was using the button as Flat, and hence the color showed properly. I removed the flat property and it doesnt draw color on the button... i tried various roles like QPalette::Button , etc to change the color, but still no help :(

Is it bec as Jpn suggested that native themes cause some effects not to have the desired effect ??

Walsi
25th April 2007, 07:23
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)



QColor myColor;
mycolor.setRgb(0, 150, 0, 255);

...
...
...

myButton->setPalette(QPalette(Qcolor(myColor)));


It works fine!

Best Regards,...

I use this code for changing the color of buttons and my operating system is KUBUNTU!
I think version 6.10

aamer4yu
26th April 2007, 05:15
Did u try it on windows ??

Walsi
26th April 2007, 08:54
What do you mean???

Compiling this SourceCode on Windows?

No I didn't try that.

Best Regards

wysota
26th April 2007, 10:52
If your code is meant to run on Windows too then I suggest you do that now and run it under WinXP.

Walsi
26th April 2007, 12:15
My code isn't suggested to run under WXP .... net yet??

Will there be much problems with colored buttons on WXP?

wysota
26th April 2007, 12:18
Just try it and you're see for yourself. If you ever plan to run the application on Windows or MacOSX, I suggest you check out the "red button issue" on all platforms ;)

Walsi
26th April 2007, 12:29
Okay,...

I will!

MarkoSan
8th December 2007, 02:14
Now, if the push button is previously set as flat, marcel's way is not working. How to change a background color of flat push button?

wysota
8th December 2007, 12:58
I won't read the whole thread again, but I'm sure the proper answer is "use stylesheets".