0
votes

I have a QImage that represents a blank piece of paper and a QPainter, which is used to paint onto this image. Sometimes, I will want to rotate/translate the QPainter before any paint operations, in order to paint onto this image in "Landscape" orientation.

Here is a simplified snippet of the code:

_image = new QImage(paperRect().size(), QImage::Format_RGB888);
_painter->begin(_image);

if (_orientation == QPrinter::Landscape)
{
    _painter->translate(0, _image->height());
    _painter->rotate(270);
}

// Painting operations here.

Unfortunately, this is not working as I had expected. It seems that even though the painter has been rotated, it is unaware of the "new" bounds it can paint within, thereby clipping to the "Portrait" size.

I have tried the following to no avail: Turning off clipping (_painter->setClipping(false);), setting a new clip rect (_painter->setClipRect(0, 0, _image.height(), _image.width());), and adjusting the window and viewport in various ways.

I have looked through the documentation of QPainter and QImage, and scoured the internet, but I haven't found this particular issue discussed before.

1
Seems like you've run into a Qt bug.Kuba hasn't forgotten Monica
A bug would imply problem with existing code, this looks more like the scenario consideration is absent from the code.dtech
What are the values from paperRect().size()? If that is still set to Portrait format (e.g. 210x294 mm for A4), that would explain a lot.JvO
@JvO The value of paperRect() depends upon the size of paper selected and resolution of the printer, and decidedly does not depend upon the selected orientation, since I am drawing onto this image sideways. Clearly my usage of the terms "orientation" and "landscape" have confused a few people, when I intended only to use these terms to help illustrate the problem.Anthony Hilyard

1 Answers

0
votes

As it turns out, the problem was unrelated to my posted code. Here is my solution, in case anyone runs into this issue in the future.

The problem originally came about during implementation of a custom QPrintEngine/QPaintEngine class. The code posted in the question works--however, I had forgotten to update the QPrintEngine::property() function to return the new dimensions corresponding to the PPK_PageRect and PPK_PaperRect keys, when the orientation was set to Landscape.

Note that the QPrintEngine::metric() function does not appear to need to be updated in this way (in my project). I'm assuming this is because the metric function is mostly used when the QPrinter that utilizes this QPrintEngine implementation is used as a paint device, and that never happens in my project.

In any case, fixing this issue allows the QImage to be properly painted on "sideways".