PySide6 打印(QPrinter)文本编辑器(QPlaintextEdit)内容

PySide6 打印(QPrinter)文本编辑器(QPlaintextEdit)内容

不足之处
  • Linux Gnome上测试不稳定
    Windows 10 稳定使用(可能是调用系统自带的打印机)
代码参考

创建基本打印功能

代码示例
bash 复制代码
# coding = utf-8

from PySide6.QtWidgets import QApplication,QWidget,QPushButton,QVBoxLayout,QPlainTextEdit
from PySide6.QtPrintSupport import QPrintDialog,QPrinter
from PySide6.QtGui import QPagedPaintDevice
import sys

class TestPrint(QWidget):

    def __init__(self):
        super().__init__()
        self.setupUi()
        self.setEventBind()

    def setupUi(self):
        self.setWindowTitle("测试打印")

        vbox = QVBoxLayout(self)

        self.plainTextEdit = QPlainTextEdit()
        self.printButton = QPushButton("打印")

        vbox.addWidget(self.plainTextEdit)
        vbox.addWidget(self.printButton)

    def setEventBind(self):
        self.printButton.clicked.connect(self.getPrintDialog)

    def getPrintDialog(self):
        """
        """
        printer = QPrinter()
        dialog = QPrintDialog(printer)
        if dialog.exec():
            self.plainTextEdit.print_(printer)



if __name__ == "__main__":
    app =QApplication(sys.argv)
    testPrint = TestPrint()
    testPrint.show()
    sys.exit(app.exec())
相关推荐
我的xiaodoujiao11 分钟前
使用PyMySQL模块技术操作MySQL数据库
数据库·python·测试工具·mysql
卷无止境30 分钟前
终端里的AI辅助,一场正在发生的编程效率变革
后端·python
苏灿烤鱼30 分钟前
上下文写成文件系统,为什么向量还能静默丢?
python·github·agent
SamChan901 小时前
对比 4 种主流 PDF 文档解析方案:PyMuPDF vs pdfplumber vs Apache PDFBox vs 大模型 OCR
python·ai·pdf·ocr·apache·机器翻译
安_9 小时前
如何构建和使用向量索引?HNSW 和 IVF 有什么区别?
python·ai
counting money10 小时前
Java IO流详解:从InputStream到文件操作实战
java·开发语言·python
坚持学习前端日记11 小时前
Python SQLAlchemy ORM 从0到1精通实战手册(基础到复杂高阶)
数据库·python·oracle
北斗落凡尘11 小时前
LangGraph 入门实战(11)--输出模式
后端·python·langchain
雪碧聊技术12 小时前
安装Python(保姆级教程)
python·版本更新·python下载
++==12 小时前
JSON和Python的 四种核心容器
python·json