forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (80 loc) · 3.04 KB
/
main.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
""" QWebEngineView in QPrintPreviewDialog"""
"""
Created on 2019-01-17 <br>
description: 摘抄自 eric6 和 https://github.com/pandel/opsiPackageBuilder/blob/c0e660ecc8d4ec8fb8dc242d2174490c5dc67930/oPB/gui/utilities.py <br>
author: [email protected] <br>
site: https://github.com/625781186 <br>
更多经典例子:https://github.com/892768447/PyQt <br>
课件: https://github.com/625781186/WoHowLearn_PyQt5 <br>
视频教程: https://space.bilibili.com/1863103/#/ <br>
"""
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtPrintSupport import *
from PyQt5.QtWebEngineWidgets import *
import sys
class HtmlView(QWebEngineView):
"""Subclass QWebView and connect to a QPrintPreviewDialog object"""
def __init__(self, url="", parent=None, ):
"""
Constructor of HtmlView
:param parent: parent of the view
:param url: url to load, if set, a loadFInished signal is emitted
"""
super().__init__(parent)
self.html = ""
self.setUrl(QUrl(url))
self.preview = QPrintPreviewDialog()
self.textedit = QTextEdit()
self.preview.paintRequested.connect(self.printPreview)
if url != "":
self.loadFinished.connect(self.execpreview)
def execpreview(self, arg):
self.preview.exec()
# -------------法一------------- ↓
## 通过将Html 写到textedit , 再将textedit渲染到printpreviewdialog
# def execpreview(self, arg):
# self.page().toHtml(self.setHtml_)
# self.preview.exec()
#
# def printPreview(self, printer):
#
# self.textedit.print(printer)
#
# def setHtml_(self, html):
#
# self.textedit.setHtml(html)
#
# # small workaround to find the QPrintPreviewWidget inside the pre-defined dialog and force it to update its content
# wdg = self.preview.findChild(QPrintPreviewWidget)
# if wdg is not None:
# wdg.updatePreview()
# -------------法一------------- ↑
def printPreview(self, printer):
# 打印机颜色
printer.setColorMode(QPrinter.GrayScale)
# 起始页?
printer.setPageOrder(QPrinter.FirstPageFirst)
# 页边距
printer.setPageMargins(
1.0 * 10, 1.0 * 10, 1.0 * 10, 1.0 * 10,
QPrinter.Millimeter
)
# 文档名
# printer.setPrinterName("打印机里显示的文档名")
# 设置DPI
printer.setResolution(150)
# ----------------------------------------------
## !需要开启事件循环 , 否则无法渲染到 printpreviewdialog
loop = QEventLoop()
QTimer.singleShot(10000, loop.quit)
self.page().print(printer,
lambda *a: loop.quit() if loop and loop.isRunning() else None)
loop.exec_()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = HtmlView(url="file:///报警记录2019-04-12 16-52-53.html")
main_window.show()
app.exec_()