3

I am trying to render a QGraphicsScene to an image using Qt5 using the following code:

QImage image(outputWidth, outputHeight, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
scene->render(&painter);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
image.mirrored().save("output.png");

The problem is that points that are too close to the boundary of the image are not rendered. Is there a way to enforce a padding/margin?

Mauricio Zambon
  • 695
  • 2
  • 9
  • 17
  • 1
    Unrelated: it makes no sense to set painter options *after* you painted. And yes, there is a way to get margins: get an image bigger than the `sceneRect`, transform the painter to offset the origin a little bit, and pass to `render` the actual drawing area. – peppe Oct 11 '16 at 21:58

1 Answers1

2

You could implement a pad by rendering to a larger image then clipping to a smaller one.

int pad_width = 4;
QImage image;
QImage padded_image(outputWidth + 2 * pad_width, outputHeight + 2 * pad_width, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&padded_image);
scene->render(&painter);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
image = padded_image.copy(pad_width, pad_width, outputWidth, outputHeight);
image.mirrored().save("output.png");
Chris
  • 574
  • 7
  • 24