QBrush transformation ignored by QPrinter/QPdfWriter
-
I am trying to produce a PDF from a QGraphicsScene.
I am drawing some paths with a brush that has a pixmap texture, and a scale transformation applied (withbrush.setTransform).
When I display the scene in a QGraphicsView I get exactly the desired output.
When I render the same scene to a QPrinter (set to produce a pdf) the texture is applied but ignoring the transformation.
Is this a known limitation? Is there a way around it? -
Hi and welcome to devnet,
Which version of Qt are you using ?
On which platform ?
Can you provide a minimal compilable example that shows that behaviour ? -
Hi! I am using PyQt 5.15.2 (Qt 5.15.2) on macOs 10.15.7.
Below is a minimal example showing the problem in a context similar to my use case.
Here I am creating a texture on the fly for illustration purposes.
The rendering artifacts due to antialiasing are irrelevant for my question.
The issue is the discrepancy between the scale (set at .2) of the texture in the rendered brush and the scale (always 1) in the pdf output.
[To generate the output just double click on the view, you'll see atest.pdfin the current path.]from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtPrintSupport import * class Test(QGraphicsView): def __init__(self): QGraphicsView.__init__(self) tr = QTransform() tr.scale(.2,.2) s = 50 img = QImage(s, s, QImage.Format_ARGB32) img.fill(Qt.transparent) for x in range(s): img.setPixelColor(x,0,Qt.black) img.setPixelColor(x,s-1,Qt.black) img.setPixelColor(s-1,x,Qt.black) img.setPixelColor(0,x,Qt.black) self.scene = QGraphicsScene() self.setScene(self.scene) r = self.scene.addRect(0, 0, 1404, 1872) pen = QPen() pen.setWidth(150) pen.setCapStyle(Qt.RoundCap) pen.setJoinStyle(Qt.RoundJoin) b=QBrush(img) b.setTransform(tr) pen.setBrush(b) path = QPainterPath(QPointF(200, 200)) path.lineTo(300,300) path.lineTo(300,1000) path.lineTo(700,700) self.pathItem = QGraphicsPathItem(path, r) self.pathItem.setPen(pen) def resizeEvent(self, event): self.fitInView(self.sceneRect(), Qt.KeepAspectRatio) def mouseDoubleClickEvent(self, event): printer = QPrinter(QPrinter.HighResolution) printer.setOutputFormat(QPrinter.PdfFormat) # printer.setPageSize(QPrinter.A4) printer.setOutputFileName("test.pdf") printer.setPaperSize(QSizeF(1404,1872), QPrinter.Millimeter) printer.setPageMargins(0,0,0,0, QPrinter.Millimeter) p=QPainter() p.begin(printer) self.scene.render(p) p.end() if __name__ == '__main__': app = QApplication([]) print(QT_VERSION_STR) print(PYQT_VERSION_STR) viewer = Test() viewer.show() app.exec_() -
Looks like you may have found something. Did you already check the bug report system ?
-
A quick search in the bug report system returned nothing related...
-
Did you also test with PySide2/6 ?
-
I did not test this with PySide myself, but another dev confirmed this problem when using PySide2 (5.14.2) to do something very similar. I'd be very surprised if this was caused by the python bindings and not by Qt itself...
-
Since these bindings are usually provided with different builds of Qt there might have been one that was working. Since it's reproducible with PySide2 it's one more reason to open a bug report with your minimal example.