[Qt-interest] QRegion with rounded rect

Andre Haupt andre at bitwigglers.org
Wed Jun 10 08:13:06 CEST 2009


Hi Samuel,

On Wed, Jun 10, 2009 at 01:06:01PM +0200, Samuel Rødal wrote:
> Andre Haupt wrote:
>> Hi all,
>>
>> Maybe i don't see the wood for the trees, but can somebody give me a hint,
>> how to construct a QRegion with a shape of a rounded rectangle?
>>
>> regards,
>>
>> Andre
>
> I'm not sure why you would want to do this, but one way would be to use  
> QPainterPath::addRoundedRect, then QPainterPath::toFillPolygon, and  
> finally the QRegion constructor that takes a polygon.
>
> Something like this:
>
> QPainterPath path;
> path.addRoundedRect(QRectF(0, 0, 10, 10), 2, 2);
>
> QRegion region(path.toFillPolygon().toPolygon());
>
> Though in most cases you'd prefer to use the original path instead of  
> the resulting region as you'd then get nice antialiased edges.

The rounded rect shaped QRegion was only an intermediate goal. I then wanted
to create another elliptic QRegion and subtract that from the round rect region.
The resulting region i use as a clip region for QPainter, and i fill that region with an
alpha gradient brush to achieve a nice glass effect (like in the embedded widgets ampere meter demo).

In the meanwhile i found a solution that works for me, but i am not sure
that my approach is "the right thing (tm)" to achieve such a glass effect.

Should i use QPainterPath instead to construct the shape and then do a QPainter::fillPath()?

regards,

Andre



More information about the Qt-interest-old mailing list