QPainter - Draw Polygon with multiple holes

1.2k Views Asked by At

I am trying to draw a polygon with multiple holes in them with QPainter (QT5.8, win64). I am using the following code:

QPainter pm(&image);
QPen p(Qt::gray, 2);
p.setCosmetic(true);
pm.setPen(p);   
pm.setBrush(QBrush(color));

QPainterPath pap;

pap.addPolygon(pObject->getOuterGeometryPolyF());

for (int i = 0; i < (int)pObject->m_InnerGeometry.size(); i++)
{
    QPainterPath papInner;
    papInner.addPolygon(pObject->getInnerGeometryPolyF(i));
    pap = pap.subtracted(papInner);
}

pm.drawPath(pap);

But it will only show one hole (see image):

enter image description here

Can someone provide me with an example of how to draw a polygon with multiple holes on it? The documentation is not clear on this point.

1

There are 1 best solutions below

0
On

I will answer my own question...

It all depends on the rotation order of the polygon (clockwise or anti clockwise). The holes should have opposite rotation order of the outer polygon to make it work flawlessly.

So for example outer polygon = clockwise, inner polygons should have anti-clockwise order. If this is not the case, reverse the order.