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())
相关推荐
智能体与具身智能12 小时前
TVA具身智能的概念、架构与应用(19)
人工智能·python·具身智能
2601_9622946113 小时前
python中range函数怎么用
python·for循环·可迭代对象·range函数·整数列表
青 春 记 忆14 小时前
零基础入门python70:Docker Compose 编排完整后端
python·后端开发
新时代牛马14 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
小灰灰搞电子14 小时前
PyQt 实现蜂巢菜单源码分享
pyqt·鱼眼凸起
插件开发14 小时前
Illustrator 插件开发:链接文件存在检查的原理与实战
ui·illustrator
白山编程大哥15 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
xy345315 小时前
axure9.0 如何打造一个计时器(简单版)
前端·ui·html·axure·原型·产品设计
落羽的落羽15 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Chasing__Dreams15 小时前
大模型应用开发--13--RAG 查询优化策略
python