PyQt5/PyQt

How do I put an image in the PrinterPreviewDialog?

K9Developer opened this issue · 2 comments

how can i put a PIL image into the PrintPreviewDialog

  1. convert PIL image to QPixmap
  2. get QPrintPreviewDialog printer()
  3. maybe you can use QPainter on printer to draw pixmap

like this:

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtPrintSupport import QPrintPreviewDialog, QPrinter
from PyQt5.QtWidgets import QApplication


def drawImage(printer):
    painter = QPainter()
    painter.begin(printer)
    painter.setPen(Qt.red)
    painter.drawText(0, 0, 'hello PyQt5')
    painter.end()


app = QApplication(sys.argv)
dlg = QPrintPreviewDialog()
dlg.printer().setOutputFormat(QPrinter.PdfFormat)
dlg.paintRequested.connect(drawImage)
dlg.exec_()
app.exec_()