Hello All,
I was wondering if there was a way in which to draw a rounded rectangle using the drawRoundedRect() function on a QPainter object in which half of the Rounded Rectangle is a Gradient and the other half is a solid color.
Thanks Alot.
Chris
Printable View
Hello All,
I was wondering if there was a way in which to draw a rounded rectangle using the drawRoundedRect() function on a QPainter object in which half of the Rounded Rectangle is a Gradient and the other half is a solid color.
Thanks Alot.
Chris
I'm afraid you have to do it in two parts by clipping half of the other rounded rect. See QPainter::setClipRect().
If anyone is interested, here's is how I solved the problem mentioned above with Clipping. I start with a (60, 60) rendering area. My objective is to paint half of a Rounded Rectangle a solid color while the other half is a gradient. I split the Rounded Rectangle from from (0, 60) to (60,0).
First I declare the two regions. The Upper Region and the Lower Region
Code:
QPainterPath UpperSidePath; QPolygonF UpperPolygon; HighSidePath.addPolygon(UpperPolygon); QQPainterPath LowSidePath; QPolygonF LowerPolygon; LowSidePath.addPolygon(LowerPolygon);
Next, I paint the two separate regions using clipping. (The Upper Region is painted in red, the lower region using a green gradient.
Code:
QPainter UpperRegion; UpperRegion.begin(this); UpperRegion.scale(width() / 60, height() / 60); UpperRegion.setClipping(true); UpperRegion.setClipPath(UpperSidePath); UpperRegion.drawRoundedRect(rectangle, 20.0, 15.0); UpperRegion.drawLine(line); UpperRegion.end(); QPainter LowerRegion; LowerRegion.begin(this); LowerRegion.scale(width() / 60, height() / 60); LowLoadedGradient.setColorAt(0.0, Qt::white); LowLoadedGradient.setColorAt(0.2, Qt::green); LowLoadedGradient.setColorAt(1.0, Qt::black); LowerRegion.setBrush(LowLoadedGradient); LowerRegion.setClipping(true); LowerRegion.setClipPath(LowSidePath); LowerRegion.drawRoundedRect(rectangle, 20.0, 15.0); LowerRegion.end();