|
| 1 | + |
| 2 | +""" QWebEngineView in QPrintPreviewDialog""" |
| 3 | +""" |
| 4 | +Created on 2019-01-17 <br> |
| 5 | +description: 摘抄自 eric6 和 https://github.com/pandel/opsiPackageBuilder/blob/c0e660ecc8d4ec8fb8dc242d2174490c5dc67930/oPB/gui/utilities.py <br> |
| 6 | + |
| 7 | +site: https://github.com/625781186 <br> |
| 8 | +更多经典例子:https://github.com/892768447/PyQt <br> |
| 9 | +课件: https://github.com/625781186/WoHowLearn_PyQt5 <br> |
| 10 | +视频教程: https://space.bilibili.com/1863103/#/ <br> |
| 11 | +""" |
| 12 | +from PyQt5 import QtGui, QtWidgets, QtCore |
| 13 | +from PyQt5.QtCore import * |
| 14 | +from PyQt5.QtGui import * |
| 15 | +from PyQt5.QtWidgets import * |
| 16 | +from PyQt5.QtPrintSupport import * |
| 17 | +from PyQt5.QtWebEngineWidgets import * |
| 18 | +import sys |
| 19 | + |
| 20 | + |
| 21 | +class HtmlView(QWebEngineView): |
| 22 | + """Subclass QWebView and connect to a QPrintPreviewDialog object""" |
| 23 | + |
| 24 | + def __init__(self, url="", parent=None, ): |
| 25 | + """ |
| 26 | + Constructor of HtmlView |
| 27 | + :param parent: parent of the view |
| 28 | + :param url: url to load, if set, a loadFInished signal is emitted |
| 29 | + """ |
| 30 | + super().__init__(parent) |
| 31 | + |
| 32 | + self.html = "" |
| 33 | + self.setUrl(QUrl(url)) |
| 34 | + |
| 35 | + self.preview = QPrintPreviewDialog() |
| 36 | + |
| 37 | + self.textedit = QTextEdit() |
| 38 | + |
| 39 | + self.preview.paintRequested.connect(self.printPreview) |
| 40 | + |
| 41 | + if url != "": |
| 42 | + self.loadFinished.connect(self.execpreview) |
| 43 | + |
| 44 | + def execpreview(self, arg): |
| 45 | + self.preview.exec() |
| 46 | + |
| 47 | + # -------------法一------------- ↓ |
| 48 | + ## 通过将Html 写到textedit , 再将textedit渲染到printpreviewdialog |
| 49 | + # def execpreview(self, arg): |
| 50 | + # self.page().toHtml(self.setHtml_) |
| 51 | + # self.preview.exec() |
| 52 | + # |
| 53 | + # def printPreview(self, printer): |
| 54 | + # |
| 55 | + # self.textedit.print(printer) |
| 56 | + # |
| 57 | + # def setHtml_(self, html): |
| 58 | + # |
| 59 | + # self.textedit.setHtml(html) |
| 60 | + # |
| 61 | + # # small workaround to find the QPrintPreviewWidget inside the pre-defined dialog and force it to update its content |
| 62 | + # wdg = self.preview.findChild(QPrintPreviewWidget) |
| 63 | + # if wdg is not None: |
| 64 | + # wdg.updatePreview() |
| 65 | + # -------------法一------------- ↑ |
| 66 | + |
| 67 | + def printPreview(self, printer): |
| 68 | + # 打印机颜色 |
| 69 | + printer.setColorMode(QPrinter.GrayScale) |
| 70 | + # 起始页? |
| 71 | + printer.setPageOrder(QPrinter.FirstPageFirst) |
| 72 | + # 页边距 |
| 73 | + printer.setPageMargins( |
| 74 | + 1.0 * 10, 1.0 * 10, 1.0 * 10, 1.0 * 10, |
| 75 | + QPrinter.Millimeter |
| 76 | + ) |
| 77 | + # 文档名 |
| 78 | + # printer.setPrinterName("打印机里显示的文档名") |
| 79 | + # 设置DPI |
| 80 | + printer.setResolution(150) |
| 81 | + # ---------------------------------------------- |
| 82 | + ## !需要开启事件循环 , 否则无法渲染到 printpreviewdialog |
| 83 | + loop = QEventLoop() |
| 84 | + QTimer.singleShot(10000, loop.quit) |
| 85 | + |
| 86 | + self.page().print(printer, |
| 87 | + lambda *a: loop.quit() if loop and loop.isRunning() else None) |
| 88 | + |
| 89 | + loop.exec_() |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + app = QtWidgets.QApplication(sys.argv) |
| 94 | + main_window = HtmlView(url="file:///报警记录2019-04-12 16-52-53.html") |
| 95 | + |
| 96 | + main_window.show() |
| 97 | + app.exec_() |
0 commit comments