0

我想打印一些由 qtdesigner 设计的文本和图像。

如何更改字体和另一种格式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_())

4

1 回答 1

0

document.print_(printer)很方便,但是灵活一点。

因为您不使用QTextEdit,所以您必须仅通过您的代码编辑格式信息。(除了预先设置的 setHtml。)如果您QPrinter喜欢绘制文档,您可以使用QPainter.

通常,虽然QPainter在 中使用,但paintEvent(self, event)只有将 的子类设置为 的QPaintDevice构造函数,才能随心所欲QPainter地绘制。但是您必须准确计算分配。

QPrinter 是QPaintDevice.

陷阱


的左上角QPainter.drawText是文本的底行。

(x, y)→</sub> 我的文本


这是代码。

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

我的结果 我的结果

如果要使用QTextCursor,可以使用 的方法设置Font & format QTextCursor

您可以通过此代码获得类似的结果。如果你不希望高控制,我推荐这个。

其他解决方案

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)
于 2020-12-31T14:06:56.270 回答