问题
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:
pls use the code and apply your program. I am not applyED your program because you used qt designer .. QT DESİGNER is a bad practice . text cursor and textdocument changed pyqt4 to pyqt5 ı am not solve your code but useful code belove...
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Pencere(QWidget):
def __init__(self):
super().__init__()
self.setUI()
def setUI(self):
self.label = QLabel("label")
button = QPushButton("write font type")
v_box = QVBoxLayout()
v_box.addWidget(self.label)
v_box.addWidget(button)
button.clicked.connect(self.changedfont)
self.setLayout(v_box)
self.setWindowTitle("QFontDialog")
self.show()
def changedfont(self):
getQFontobject,state = QFontDialog.getFont()
if state:
self.label.setFont(getQFontobject)
if __name__ == "__main__":
app = QApplication(sys.argv)
pencere = Pencere()
sys.exit(app.exec())
and
other link is a ı think useful: Qt4/PyQt4 - Can not set the default font for QTextDocument
https://doc.qt.io/qt-5/qtextdocument.html#setDefaultFont https://doc.qt.io/archives/qt-4.8/qtextdocument.html
and I am not solve AttributeError: 'Ui_MainWindow' object has no attribute 'document' but you can changed code ...
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
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):
self.document = QtGui.QTextDocument()
self.cursor = QtGui.QTextCursor(self.document)
#self.cursor.insertImage('logo4.jpg')
self.cursor.insertBlock()
self.cursor.insertText('MY TEXT')
self.document.print_(printer)
# mycode mycode :)
def changedfont(self):
getQFontobject,state = QFontDialog.getFont()
if state:
#c_format = QtGui.QTextCharFormat()
#c_format.setFont(getQFontobject)
self.document.setDefaultFont(getQFontobject)
#self.cursor.setCharFormat(c_format)
#cur.insertText('sample text')
#self.document.setDefaultFont(getQFontobject)
#self.document.setFont(getQFontobject)
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")
# mycode
self.pushButton_font = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_font.setObjectName("pushbutton_font")
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)
############################################################
######### mycode #############
self.pushButton_font.clicked.connect(self.changedfont)
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_())
回答2:
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
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)
来源:https://stackoverflow.com/questions/64403292/how-to-change-the-font-and-size-of-image-in-qtextdocument