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

Israel Brewster ijbrewster at alaska.edu
Thu Apr 7 22:04:08 CEST 2022


On Apr 7, 2022, at 11:58 AM, Henry Skoglund <henry at tungware.se> wrote:
> 
> 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)

No change. The 1 value was picked because that’s what displays correctly in testing, but even with the suggested change I still get a solid white box.
---
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

> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest



More information about the Interest mailing list