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

Henry Skoglund henry at tungware.se
Thu Apr 7 21:58:39 CEST 2022


On 2022-04-07 20:28, Israel Brewster wrote:
> 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()
>

Hi, try
...
gradient_rect = QRect(0, 0, 500, 25)
gradient = QLinearGradient(0, 0, 500, 25)
gradient.setColorAt(0, Qt.blue)
gradient.setColorAt(1, Qt.red)
...
(i.e. QLinearGradient's ctor expects coords)



More information about the Interest mailing list