I have a PlotWidget containing (among other things) a plot (created via plotItem.plot() and a textItem (created directly and added via addItem). Both plot and textItem have ZValue set. If the textItem has a zvalue equal to or greater than the plot, everything is fine. However, if the textItem has a zValue *less* than the plot, then when generating a PDF the plot is drawn shifted slightly, such that it protrudes outside the view box. In both cases, the widgets display fine, it’s only the PDF output that is messed up. See attached images and sample code.

I don’t know yet if this is a problem with the underlying QGraphicsView, or something specific to PyQtGraph. Anyone have any idea how I can fix this (other than always putting the labels on top)?

from PySide6.QtWidgets import QApplication, QDialog, QVBoxLayout
from PySide6.QtCore import QPoint, QSizeF, QPointF, QMarginsF, QSize
from PySide6.QtGui import QPicture, QPainter, QPageSize
from PySide6.QtPrintSupport import QPrinter
import pyqtgraph as pg

def create_pdf(display_widget, file): 
        qpic = QPicture()
        pic_painter = QPainter(qpic)
        display_widget.render(pic_painter, QPoint())
        pic_painter.end()

        page_size = QPageSize(QSizeF(11, 8.5), QPageSize.Inch,
                              matchPolicy = QPageSize.ExactMatch)

        printer = QPrinter()

        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setPageSize(page_size)
        printer.setResolution(dpi)
        printer.setOutputFileName(file)
        printer.setPageMargins(QMarginsF(0, 0, 0, 0))

        painter = QPainter(printer)
        painter.drawPicture(QPointF(0, 0), qpic)
        painter.end()

app = QApplication()

dpi = int(QApplication.primaryScreen().logicalDotsPerInch())
pixel_size = QSize(round(11 *dpi), round(8.5 *dpi))

display_widget = QDialog()
display_widget.setFixedSize(pixel_size)

display_layout = QVBoxLayout()
display_layout.setContentsMargins(15, 15, 50, 15)
display_widget.setLayout(display_layout)

pw = pg.PlotWidget()
plot_item = pw.getPlotItem()

display_layout.addWidget(pw)

# Doesn't matter, from what I can tell
test_data = range(-50, 50)

# Plot some random data, but give it a raised zValue so it plots on top of others
plot = plot_item.plot(test_data, test_data, pen = '#00F')
plot.setZValue(1)

#Restrict the X range so the plot goes out-of-view on both ends
pw.setXRange(-20, 20, padding = 0)

test_label = pg.TextItem("LABEL", '#EEE')
test_label.setPos(0, 1.25)
test_label.setZValue(0) # Below the plot produces bad output
plot_item.addItem(test_label, ignoreBounds = True)

#If you want to see the image prior to PDF generation
#display_widget.show()
#app.exec()

create_pdf(display_widget, '/tmp/bad_output.pdf')
test_label.setZValue(2) # Move the text item to the top of the draw stack
create_pdf(display_widget, '/tmp/good_output.pdf')

Attachment: bad_output.pdf
Description: Adobe PDF document

Attachment: good_output.pdf
Description: Adobe PDF document


---
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@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to