How to change the font and size of image in QTextDocument?

705 views Asked by At

I want to print some text and image designed by qtdesigner.

How could I change font and another format in QTextDocument?

from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport


class Ui_MainWindow(object):

    def handlePrint(self):
        dialog = QtPrintSupport.QPrintDialog()
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            self.handlePaintRequest(dialog.printer())

    def handlePreview(self):
        dialog = QtPrintSupport.QPrintPreviewDialog()
        dialog.paintRequested.connect(self.handlePaintRequest)
        dialog.exec_()

    def handlePaintRequest(self, printer):
        document = QtGui.QTextDocument()
        cursor = QtGui.QTextCursor(document)
        cursor.insertImage('logo4.jpg')
        cursor.insertBlock()
        cursor.insertText('MY TEXT')
        
        document.print_(printer)


    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton_print_previw = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_print_previw.setGeometry(QtCore.QRect(90, 270, 251, 91))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.pushButton_print_previw.setFont(font)
        self.pushButton_print_previw.setObjectName("pushButton_print_previw")
        self.pushButton_print = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_print.setGeometry(QtCore.QRect(440, 270, 251, 91))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.pushButton_print.setFont(font)
        self.pushButton_print.setObjectName("pushButton_print")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        ############################################################
        self.pushButton_print_previw.clicked.connect(self.handlePreview)
        ############################################################

        ############################################################
        self.pushButton_print.clicked.connect(self.handlePrint)
        ############################################################



    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton_print_previw.setText(_translate("MainWindow", "Print Preview"))
        self.pushButton_print.setText(_translate("MainWindow", "Print"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1

There are 1 answers

0
Haru On

document.print_(printer) is very convenient but it is not a little flexible.

Because you don't use QTextEdit, you must edit the format information by your code only. (except for setHtml in advance.) If you draw QPrinter document as you like, you can use QPainter.

Normarly, although QPainter is used in paintEvent(self, event), only if you set a subclass of QPaintDevice to constructor of QPainter, you can draw as you like. But you must calculate the allocation exactly.

QPrinter is a subclass of QPaintDevice.

Trap


The topleft of QPainter.drawText is the bottom line of Text.

(x, y)→ MY TEXT


Here is the code.

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport


class Ui_MainWindow(object):

    def handlePrint(self):
        dialog = QtPrintSupport.QPrintDialog()
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            self.handlePaintRequest(dialog.printer())

    def handlePreview(self):
        dialog = QtPrintSupport.QPrintPreviewDialog()
        dialog.paintRequested.connect(self.handlePaintRequest)
        dialog.exec_()

    def handlePaintRequest(self, printer):
        document = QtGui.QTextDocument()
#        cursor = QtGui.QTextCursor(document)
#        cursor.insertImage('logo4.jpg')
#        cursor.insertBlock()
#        cursor.insertText('MY TEXT')
#        document.print_(printer)
        documentRootFrameFormat = document.rootFrame().frameFormat()
        documentLeftMargin = documentRootFrameFormat.leftMargin()
        documentTopMargin = documentRootFrameFormat.topMargin()
        drawn_im = QtGui.QImage("logo4.jpg")
        drawn_im_height = drawn_im.height()        

        painter = QtGui.QPainter(printer)
        painter.drawImage(documentLeftMargin, documentTopMargin, drawn_im)
        painter_font = painter.font()
        #you can set flag which font is used.
        custom_font = QtGui.QFont("Segoe UI Light", 18)        
        fontMetricsF = QtGui.QFontMetricsF(custom_font)
        painter.save()
        painter.setFont(custom_font)
        height_fontMetricsF = fontMetricsF.height()
        painter.drawText(documentLeftMargin, documentTopMargin + drawn_im_height + height_fontMetricsF, 'MY TEXT')
        painter.restore()



    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton_print_previw = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_print_previw.setGeometry(QtCore.QRect(90, 270, 251, 91))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.pushButton_print_previw.setFont(font)
        self.pushButton_print_previw.setObjectName("pushButton_print_previw")
        self.pushButton_print = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_print.setGeometry(QtCore.QRect(440, 270, 251, 91))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.pushButton_print.setFont(font)
        self.pushButton_print.setObjectName("pushButton_print")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        ############################################################
        self.pushButton_print_previw.clicked.connect(self.handlePreview)
        ############################################################

        ############################################################
        self.pushButton_print.clicked.connect(self.handlePrint)
        ############################################################



    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton_print_previw.setText(_translate("MainWindow", "Print Preview"))
        self.pushButton_print.setText(_translate("MainWindow", "Print"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

My Result My Result

If you want to use QTextCursor, you can set Font & format by using the method of QTextCursor.

You can get the similar result by this code. I recommend this if you don't hope high control.

Other Solution

def handlePaintRequest(self, printer):
    document = QtGui.QTextDocument()
    cursor = QtGui.QTextCursor(document)
    
    cursor.insertImage("logo4.jpg")
    cursor.insertBlock()
    charFormat = cursor.charFormat()
    charFormat.setFont(QtGui.QFont("Segoe UI Light", 18))
    cursor.insertText('MY TEXT', charFormat)

    document.print_(printer)