PDA

View Full Version : QPushButton color and background



satanowicz
25th May 2010, 18:19
What's the simplest way to change a button fonts color & background?
I've tried style sheets and came up with something like this (but it doesn't work)


QPushButton *friends = new QPushButton("Friends", imageLabel);
friends->setFlat(true);
QObject::connect(friends, SIGNAL(clicked()),
this, SLOT(updateHitPoints()));
friends-> move(180,270);

friends->setStyleSheet("background-color: yellow");

JD2000
25th May 2010, 18:57
What doesn't work?

satanowicz
25th May 2010, 19:40
the button has yellow bacground only when it is pressed
is it because the

friends->setFlat(true); ?
if yes- can i have a "flat" button with yellow background?

tbscope
25th May 2010, 19:46
To quote the documentation:


If this property is set, most styles will not paint the button background unless the button is being pressed.
So yes, when you use setFlat, it doesn't work the way you want.

Try using QPushButton:flat in your stylesheet, and set the background in there.

However, why do you even want to use setFlat (why did Nokia even include it?). You can get something completely similar using a stylesheet.

http://doc.qt.nokia.com/4.6/stylesheet-examples.html#customizing-qpushbutton

satanowicz
25th May 2010, 21:56
didn't know about that feature, thanks

one more thing: how do You include a .qss file in a VS 2008 project? so it can be properly compiled?