diff --git a/QTextBrowser/DynamicRes.py b/QTextBrowser/DynamicRes.py new file mode 100644 index 0000000..634a57b --- /dev/null +++ b/QTextBrowser/DynamicRes.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2020/6/3 +@author: Irony +@site: https://pyqt.site https://github.com/PyQt5 +@email: 892768447@qq.com +@file: DynamicRes +@description: +""" +from threading import Thread + +import requests +from PyQt5.QtCore import QUrl, QByteArray +from PyQt5.QtGui import QImage, QTextDocument +from PyQt5.QtWidgets import QTextBrowser, QWidget, QVBoxLayout, QPushButton + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2020' +__Version__ = 'Version 1.0' + + +class TextBrowser(QTextBrowser): + NetImages = {} + + def __init__(self, *args, **kwargs): + super(TextBrowser, self).__init__(*args, **kwargs) + self.setOpenLinks(False) # 禁止打开URL + + def downloadImage(self, url): + try: + self.NetImages[url] = [QByteArray(requests.get(url.toString()).content), 1] + print('下载完成', url) + except Exception as e: + print('下载失败', url, e) + self.NetImages[url] = [QByteArray(), 1] + + def loadResource(self, rtype, url): + ret = super(TextBrowser, self).loadResource(rtype, url) + # 加载图片资源 + if rtype == QTextDocument.ImageResource: + if ret: + return ret + if url.toString().startswith('irony'): # 自定义的协议头 + print('加载本地', '../Donate/zhifubao.png', url) + return QImage('../Donate/zhifubao.png') # 或者 QByteArray(open('../Donate/zhifubao.png', 'rb').read()) + elif url.toString().startswith('http'): # 加载网络图片 + img, status = self.NetImages.get(url, [None, None]) + if url not in self.NetImages or status is None: + # 子线程下载 + self.NetImages[url] = [None, 1] + print('download ', url) + Thread(target=self.downloadImage, args=(url,), daemon=True).start() + elif img: + return img + return ret + + def mouseDoubleClickEvent(self, event): + # 双击图片得到图片的URL,也可以用来放大显示 + super(TextBrowser, self).mouseDoubleClickEvent(event) + url = self.anchorAt(event.pos()) + if url: + print('url:', url, self.document().resource(QTextDocument.ImageResource, QUrl(url))) + + +class Window(QWidget): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + layout = QVBoxLayout(self) + + self.textBrowser = TextBrowser(self) + self.downButton = QPushButton('加载网络图片', self) + + layout.addWidget(self.textBrowser) + layout.addWidget(self.downButton) + + # 加载本地图片 + img = QImage('../Donate/weixin.png') + # 第二个参数为任意唯一的url类似于qrc方式 + self.textBrowser.document().addResource(QTextDocument.ImageResource, QUrl('dynamic:/images/weixin.png'), img) + + # 设置html + # 需要注意里面的图片地址 + self.textBrowser.setHtml( + '
' # 方式一直接加载本地图片 + '' # 方式二通过addResource添加资源 + '' # 方式三定义自定义的协议头通过loadResource动态加载 + '') + + +if __name__ == '__main__': + import sys + import cgitb + + cgitb.enable(1, None, 5, '') + from PyQt5.QtWidgets import QApplication + + app = QApplication(sys.argv) + w = Window() + w.show() + sys.exit(app.exec_()) diff --git a/QTextBrowser/README.md b/QTextBrowser/README.md index e69de29..dee5984 100644 --- a/QTextBrowser/README.md +++ b/QTextBrowser/README.md @@ -0,0 +1,14 @@ +# QTextBrowser + +- 目录 + - [动态加载图片](#1动态加载图片) + +## 1、动态加载图片 +[运行 DynamicRes.py](DynamicRes.py) + +动态加载资源有多种方式,这里主要介绍 [addResource](https://doc.qt.io/qt-5/qtextdocument.html#addResource) 和 [loadResource](https://doc.qt.io/qt-5/qtextbrowser.html#loadResource) 函数 + +1、通过 `self.textBrowser.document().addResource(QTextDocument.ImageResource, QUrl('dynamic:/images/weixin.png'), img)` 向文档中注册新的资源索引,类似QRC +2、通过重载 `loadResource` 函数可以监听到所有的资源加载,然后动态返回内容 + +![DynamicRes](ScreenShot/DynamicRes.gif) \ No newline at end of file diff --git a/QTextBrowser/ScreenShot/DynamicRes.gif b/QTextBrowser/ScreenShot/DynamicRes.gif new file mode 100644 index 0000000..9c923b5 Binary files /dev/null and b/QTextBrowser/ScreenShot/DynamicRes.gif differ diff --git a/README.md b/README.md index d038768..dd55823 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分 - [显示.9格式图片(气泡)](QLabel/NinePatch.py) - [圆形图片](QLabel/CircleImage.py) - [QTextBrowser](QTextBrowser) + - [动态加载图片](QTextBrowser/DynamicRes.py) - [QGraphicsView](QGraphicsView) - [绘制世界地图](QGraphicsView/WorldMap.py) - [添加QWidget](QGraphicsView/AddQWidget.py)