| |
| from PySide2.QtWidgets import (QApplication, QWidget, QTableWidget, QPushButton, QVBoxLayout, QTableWidgetItem) |
| from PySide2.QtGui import QPixmap, QPainter, QImage, QTextDocument |
| from PySide2.QtPrintSupport import QPrinter, QPrintDialog, QPrintPreviewDialog |
| from PySide2.QtCore import QRect, QPoint, QSize, Qt |
| |
| def on_htmlButton_clicked(): |
| printer = QPrinter(QPrinter.HighResolution) |
| |
| preview = QPrintPreviewDialog(printer, widget) |
| preview.paintRequested.connect(printHtml) |
| |
| preview.exec_() |
| def printHtml(printer): |
| html = '''<html> |
| <head>文本打印测试</head> |
| <body><h1>文本打印测试</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> |
| <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> |
| </body> |
| </html> |
| ''' |
| textDocument = QTextDocument() |
| textDocument.setHtml(html) |
| |
| textDocument.print_(printer) |
| |
| def on_picButton_clicked(): |
| printer = QPrinter(QPrinter.HighResolution) |
| preview = QPrintPreviewDialog(printer, widget) |
| ''' |
| * QPrintPreviewDialog类提供了一个打印预览对话框,里面功能比较全, |
| * paintRequested(QPrinter *printer)是系统提供的, |
| * 当preview.exec()执行时该信号被触发, |
| * plotPic(QPrinter *printer)是用户自定义的槽函数,图像的绘制就在这个函数里。 |
| ''' |
| preview.paintRequested.connect(plotPic) |
| preview.exec() |
| |
| |
| def plotPic(printer): |
| painter = QPainter(printer); |
| image = QPixmap() |
| image = widget.grab(QRect(QPoint(0, 0), QSize(widget.size().width(), widget.size().height() ) ) ) |
| |
| |
| rect = painter.viewport(); |
| |
| size = image.size(); |
| size.scale(rect.size(), Qt.KeepAspectRatio) |
| |
| painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
| painter.setWindow(image.rect()); |
| painter.drawPixmap(0, 0, image); |
| |
| |
| import sys |
| app = QApplication(sys.argv) |
| tablewidget = QTableWidget() |
| |
| tablewidget.setColumnCount(4) |
| tablewidget.horizontalHeader().setDefaultSectionSize(150) |
| |
| header = ['name', 'last modify time', 'type', 'size'] |
| tablewidget.setHorizontalHeaderLabels(header) |
| tablewidget.insertRow(0) |
| tablewidget.insertRow(0) |
| pItem1 = QTableWidgetItem('aa') |
| pItem2 = QTableWidgetItem('bb') |
| pItem3 = QTableWidgetItem('cc') |
| pItem4 = QTableWidgetItem('dd') |
| pItem5 = QTableWidgetItem('8.34') |
| pItem6 = QTableWidgetItem('9.26') |
| pItem7 = QTableWidgetItem('11.99') |
| |
| tablewidget.setItem(0, 0, pItem1) |
| tablewidget.setItem(0, 1, pItem2) |
| tablewidget.setItem(0, 2, pItem3) |
| tablewidget.setItem(0, 3, pItem4) |
| tablewidget.setItem(1, 0, pItem5) |
| tablewidget.setItem(1, 1, pItem6) |
| tablewidget.setItem(1, 3, pItem7) |
| tablewidget.setMinimumSize(800, 600) |
| button = QPushButton('打印界面') |
| button.clicked.connect(on_picButton_clicked) |
| button_txt = QPushButton('打印文字') |
| button_txt.clicked.connect(on_htmlButton_clicked) |
| widget = QWidget() |
| layout = QVBoxLayout(widget) |
| layout.addWidget(button) |
| layout.addWidget(button_txt) |
| layout.addWidget(tablewidget) |
| widget.show() |
| sys.exit(app.exec_()) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from PySide2.QtCore import Qt |
| from PySide2.QtGui import QImage, QIcon, QPixmap |
| from PySide2.QtWidgets import QApplication, QMainWindow, QLabel, QSizePolicy, QAction |
| from PySide2.QtPrintSupport import QPrinter, QPrintDialog |
| import sys |
| |
| class MainWindow(QMainWindow): |
| def __init__(self, parent=None): |
| super(MainWindow, self).__init__(parent) |
| self.setWindowTitle(self.tr("打印图片")) |
| |
| self.imageLabel = QLabel() |
| self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) |
| self.setCentralWidget(self.imageLabel) |
| self.image = QImage() |
| |
| self.createActions() |
| self.createMenus() |
| self.createToolBars() |
| |
| if self.image.load("./扬帆起航共创未来2021.jpg"): |
| self.imageLabel.setPixmap(QPixmap.fromImage(self.image)) |
| self.resize(self.image.width(), self.image.height()) |
| def createActions(self): |
| self.PrintAction = QAction(QIcon("./print.bmp"), self.tr("打印"), self) |
| self.PrintAction.setShortcut("Ctrl+P") |
| self.PrintAction.setStatusTip(self.tr("打印")) |
| self.PrintAction.triggered.connect(self.slotPrint) |
| def createMenus(self): |
| PrintMenu = self.menuBar().addMenu(self.tr("打印")) |
| PrintMenu.addAction(self.PrintAction) |
| def createToolBars(self): |
| fileToolBar = self.addToolBar("Print") |
| fileToolBar.addAction(self.PrintAction) |
| def slotPrint(self): |
| printer = QPrinter() |
| printDialog = QPrintDialog(printer, self) |
| ''' |
| 判断打印对话框显示后用户是否单击“打印”按钮,若单击“打印”按钮, |
| 则相关打印属性可以通过创建QPrintDialog对象时使用的QPrinter对象获得, |
| 若用户单击“取消”按钮,则不执行后续的打印操作。 |
| ''' |
| if printDialog.exec_(): |
| |
| painter = QPainter(printer) |
| rect = painter.viewport() |
| size = self.image.size() |
| size.scale(rect.size(), Qt.KeepAspectRatio) |
| painter.setViewport(rect.x(), rect.y(), size.width(), size.height()) |
| |
| painter.setWindow(self.image.rect()) |
| painter.drawImage(0, 0, self.image) |
| If __name__ == "__main__": |
| app = QApplication(sys.argv) |
| main = MainWindow() |
| main.show() |
| sys.exit(app.exec_()) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了