c++qtopenstreetmapqpainterpathqpolygon

Transparency of overlapping polygons in Qt


Is there an easy way to make an overlapping area between two polygons transparent? Preferably built-in functions within the Qt widgets/Qt 2D graphics C++ framework. (not QML)

I'm rendering map data extracted from Openstreetmap.org and some advanced relations between ways (line segments) are given as inner and outer ways that form polygons.

For example, a lake can have outer ways that form the polygon, and several inner ways that form islands within the lake.

I am able to correctly build the outer polygons and inner polygons separately, but I struggle to find an easy way to make the area that overlaps with inner polygons transparent.

In the image below, the white areas are separate polygons drawn with white, but instead they should be transparent areas of the lake / blue polygon.

White should be transparent area of blue

I tried to use QPainterPath for the outer polygons and call

QPainterPath QPainterPath::subtracted(const QPainterPath &inner) const

but the result was a blank screen.

Do I have to create new polygons myself prior to rendering?

Should the QPainterPath solution work and I messed something up?

Is there some magic that can be done using QPen/QBrush to paint overlapping areas transparent?

Any tips on how to approach this is highly appreciated.


Solution

  • It seems I messed up the QPainterPath-approach. It works now.

    I got it working with QPolygonF directly, as it has a subtracted() method similar to QPainterPath. However, the implementation of subtracted in QPolygonF left artifacts as lines between start vertex of every inner polygon to the start vertex of the outter polygon.

    Using QPainterPath::addPolygon and performing the subtraction worked flawlessly.

    Ref. https://bugreports.qt.io/browse/QTBUG-53720 http://www.qtcentre.org/threads/19463-Draw-polygon-with-holes