[Interest] painter.fillRect() with gradient on PDF printer produces all white?

Israel Brewster ijbrewster at alaska.edu
Thu Apr 7 20:28:49 CEST 2022


Using Qt 5.15 and PySide2, I am working on a project that requires me to produce a PDF output. My process so far is to create a QWidget containing the content I want, render it to a QPicture (so it won’t be rasterized upon “printing”), then create a PDF QPrinter and use the painter.drawPicture() function to “print” the widget into a PDF.

This works well, giving me a high-quality vectorized PDF output, with one exception: any item that is filled with a gradient. When printing to PDF, the gradient always comes out as solid white. With some digging, I determined that this is even the case when doing nothing but a simple painter.fillRect() with a gradient.

Is there any “fix” for this? Thanks.

If it helps, here is some simple code that reproduces the issue:

from PySide2.QtWidgets import QApplication
from PySide2.QtGui import QPainter, QPageSize, QLinearGradient
from PySide2.QtPrintSupport import QPrinter
from PySide2.QtCore import QSize, QRect, Qt

app = QApplication()

gradient_rect = QRect(0, 0, 500, 25)
gradient = QLinearGradient(0, 0, 1, 0)
gradient.setColorAt(0, Qt.blue)
gradient.setColorAt(1, Qt.red)

page_size = QPageSize(QSize(500, 25), matchPolicy = QPageSize.ExactMatch)
printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setPageSize(page_size)
printer.setOutputFileName('/tmp/testPDFGradient.pdf')

painter = QPainter(printer)
painter.fillRect(gradient_rect, gradient)
painter.end()

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell:  907-328-9145

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220407/c0c8c243/attachment.htm>


More information about the Interest mailing list