PDA

View Full Version : gradient background



drkbkr
29th March 2006, 16:40
Hello,
If someone could help me with getting a gradient in the background of a button, I'd appreciate it.

I would expect the following code to make the button's background be a gradient from red to white vertically from the top of the button to the bottom.



QPushButton *b = new QPushButton("Hi", this);

QLinearGradient linearGrad(0, 0, 0, b->height());
linearGrad.setColorAt(0, Qt::red);
linearGrad.setColorAt(1, Qt::white);

QPalette palette = b->palette();
palette.setBrush(QPalette::Button, QBrush(linearGrad));
b->setPalette(palette);


All it does, though, is make the button black. Since it makes the button black, I know that I am successfully changing the palette and reapplying it, but I'm just not changing it in the right way.

Any help would be great.

Thanks,
Derek

drkbkr
30th March 2006, 16:22
Can I only get this effect on a button by using a QStyle?

wysota
30th March 2006, 16:31
You can always subclass the button and reimplement its paint event. Painting a button with a gradient is very tricky, so it's possible that it can't be done without subclassing.

jacek
30th March 2006, 16:37
http://www.trolltech.com/developer/tasktracker.html?method=entry&id=83921

drkbkr
30th March 2006, 17:10
Thank you. I set my QStyle to CDE and it worked fine.

Derek