PyQt5 / PyQt

PyQt Examples(PyQt各种测试和例子) PyQt4 PyQt5

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I put an image in the PrinterPreviewDialog?

K9Developer opened this issue · comments

commented

how can i put a PIL image into the PrintPreviewDialog

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

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_()