This demo program shows how to create a rich text document with an image and print preview the contents.
The Output:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | from PyQt6.QtWidgets import QWidget, QPushButton, QApplication, QTextEdit from PyQt6.QtCore import Qt from PyQt6 import QtPrintSupport import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.etext = QTextEdit(self) self.etext.setReadOnly(True) self.etext.setGeometry(25, 40, 300, 300) redText = "<span style=\" font-size:8pt; font-weight:600; color:#ff0000;\" >" redText = redText + "I want this text red" redText = redText + "</span>" self.etext.append(redText) blackText = "<p><span style=' font-size:12pt;'>CENTER</span></p>" self.etext.append(blackText) self.etext.setAlignment(Qt.AlignmentFlag.AlignCenter) img = "<p><img src='balloon.png' style='width:50px;height:60px;' ></p>" self.etext.append(img) self.etext.setAlignment(Qt.AlignmentFlag.AlignRight) self.redb = QPushButton('Preview', self) self.redb.setGeometry(250, 370, 80, 40) self.redb.clicked.connect(self.handlePreview) self.setGeometry(200, 100, 350, 430) self.setWindowTitle('Post 33') self.show() def handlePreview(self): dialog = QtPrintSupport.QPrintPreviewDialog() dialog.paintRequested.connect(self.etext.print) dialog.exec() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) if __name__ == '__main__': main() |
No comments:
Post a Comment