[Qt-interest] QRegion with rounded rect
Samuel Rødal
sroedal at trolltech.com
Thu Jun 11 09:20:18 CEST 2009
Andre Haupt wrote:
> 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
Yep, I would use QPainter::fillPath() for that. Potentially since you
have two shapes that you want to combine (the rounded rect and the
ellipse) you might consider using clip for one of them and
QPainter::fillPath() for the other. However, if in this case one of the
shape is fully contained in the other you might add them both to the
same path, thus utilizing the Qt::OddEvenFill fill rule to mask out the
inner ellipse. If possible, this should be the most optimal way.
Note that whenever you have a path that you want to clip with it's
better to use QPainter::setClipPath() instead of converting it to a
region and using QPainter::setClipRegion(). A region is composed of
integer-aligned rectangles and thus you won't get clip antialiasing in
the paint engines that support it.
Regards,
Samuel
More information about the Qt-interest-old
mailing list