打印预览

This commit is contained in:
625781186 2019-04-13 12:51:20 +08:00
parent dd63a31ec9
commit ea5e8047f1
6 changed files with 1242 additions and 0 deletions

View file

@ -46,8 +46,10 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
- [删除自定义Item](QListWidget/DeleteCustomItem.py)
- [自定义可拖拽Item](QListWidget/DragDrop.py)
- [腾讯视频热播列表](QListWidget/HotPlaylist.py)
- [在item中添加图标](Test/partner_625781186/13.combo_listwidget)
- [QTreeWidget](QTreeWidget)
- [通过json数据生成树形结构](QTreeWidget/ParsingJson.py)
- [拖拽显示为图片](Test/partner_625781186/12.1拖拽显示为图片)
- [QTableWidget](QTableWidget)
- [Sqlalchemy动态拼接字段查询显示表格](QTableWidget/SqlQuery.py)
@ -110,6 +112,8 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
- [获取Cookie](QWebView/GetCookie.py)
- [QWebEngineView](QWebEngineView)
- [获取Cookie](QWebEngineView/GetCookie.py)
- [浏览器下载文件](Test/partner_625781186/6.QWebEngineView下载文件)
- [打印网页](Test/partner_625781186/17_打印预览qwebengineview)
- [QThread](QThread)
- [继承QThread](QThread/InheritQThread.py)
@ -132,12 +136,14 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
- [右键菜单动画](QPropertyAnimation/MenuAnimation.py)
- [点阵特效](QPropertyAnimation/RlatticeEffect.py)
- [页面切换/图片轮播动画](QPropertyAnimation/PageSwitching.py)
- [折叠动画](Test/partner_625781186/2.折叠控件)
- Others
- [QFont](QFont)
- [加载自定义字体](QFont/AwesomeFont.py)
- [QMenu](QMenu)
- [菜单设置多选并且不关闭](QMenu/MultiSelect.py)
- [悬停菜单](Test/partner_625781186/5.hoverMenu)
- [QAxWidget](QAxWidget)
- [显示Word、Excel、PDF文件](QAxWidget/ViewOffice.py)
- [QSplitter](QSplitter)

View file

@ -0,0 +1,6 @@
### 17 QWebEngineView + QPrintPreviewDialog
Preview QWebEngineView.page() in QPrintPreviewDialog and print them authentically .
![1](ScreenShot/1.jpg)

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,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: 625781186@qq.com <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_()