PDA

View Full Version : QPainter - drawRoundedRect Question



ChrisReath
13th May 2008, 21:47
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

jpn
13th May 2008, 21:54
I'm afraid you have to do it in two parts by clipping half of the other rounded rect. See QPainter::setClipRect().

ChrisReath
14th May 2008, 14:14
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




QPainterPath UpperSidePath;
QPolygonF UpperPolygon;
UpperPolygon << QPointF(0,0) << QPointF(60,0) << QPointF(0,60);
HighSidePath.addPolygon(UpperPolygon);

QQPainterPath LowSidePath;
QPolygonF LowerPolygon;
LowerPolygon << QPointF(60,0) << QPointF(0,60) << QPointF(60,60);
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.




QPainter UpperRegion;
UpperRegion.begin(this);
UpperRegion.setRenderHint(QPainter::Antialiasing);
UpperRegion.scale(width() / 60, height() / 60);
UpperRegion.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
UpperRegion.setClipping(true);
UpperRegion.setClipPath(UpperSidePath);
UpperRegion.setBrush(QColor(0xff, 0x00, 0x04));
UpperRegion.drawRoundedRect(rectangle, 20.0, 15.0);
UpperRegion.drawLine(line);
UpperRegion.end();

QPainter LowerRegion;
LowerRegion.begin(this);
LowerRegion.setRenderHint(QPainter::Antialiasing);
LowerRegion.scale(width() / 60, height() / 60);
LowerRegion.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
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();