Important: Please read the Qt Code of Conduct -

Enhancing performance drawing polygons with many holes

  • I'm drawing complex polygons with many vertices and holes.
    Code sample:

    QImage image;    // some image
    QPainter painter;
    // overall region
    QPolygon polygon;
    polygon.setPoints(polyCounts, (int*)polyPoints);
    QRegion region(polygon);
    // cutting holes
    while ( holesCount-- > 0 )
        QPolygon subtractedPolygon;
        subtractedPolygon.setPoints(holePointsCount, (int*)holePoints);
        QRegion subtractedRegion(subtractedPolygon);
        region = region.subtract(subtractedRegion);
      // defining next hole properties...
    // setting clip region
    painter.setClipRegion(region, Qt::IntersectClip);
    // painting
    painter.fillRect( image.size(), Qt::red );

    It seems to be that main performance leaks are caused by copying points into QPolygon.
    I've tried using composition mode QPainter::CompositionMode_SourceOut: it works really faster, but I can't use it because I have to save original image contents.
    In Windows API PolyPolygon function seems to be useful in this situation:
    Is there any way in Qt to improve performance in this case?

  • Lifetime Qt Champion

    would it possible to setup the QPolygon and QRegion outside of
    paint so it didnt have to recreate it over and over for each paint?

  • Hi
    I use different points every rendering so it doesn't prevent from adding new points into QPolygon every painting operation.

  • Lifetime Qt Champion

    ok so not really possible to cache it ?

  • If you want better performance, you might want to look at using openGL.

Log in to reply