
本文档旨在解决在使用 PyQt5 的 QWebEngineView 组件时,如何动态更新其显示的 HTML 内容的问题。我们将提供一个完整的示例,演示如何通过信号与槽机制以及多线程来安全、高效地更新 QWebEngineView 的内容,避免阻塞主线程,确保应用程序的流畅性。
QWebEngineView 是 PyQt5 中用于显示 Web 内容的组件。直接在主线程中调用 setHtml() 方法更新内容,尤其是在加载大型 HTML 文件时,可能会导致界面卡顿。此外,频繁的 stop() 和 reload() 调用并不能保证内容一定会被更新。
为了解决上述问题,我们可以使用 PyQt5 的信号与槽机制,结合多线程来异步更新 QWebEngineView 的内容。
创建一个 Worker 类,继承自 QObject,用于在独立的线程中加载 HTML 内容并发出信号。
立即学习“前端免费学习笔记(深入)”;
from PyQt5.QtCore import pyqtSignal, QObject
import threading
class Worker(QObject):
htmlChanged = pyqtSignal(str)
def execute(self, html):
threading.Thread(target=self.task, daemon=True, args=([html])).start()
def task(self, html):
self.htmlChanged.emit(html)修改 Example 类,将 HTML 加载和更新操作移到 Worker 线程中。
在思高数码商城的基础上经过大量修改完善而成,引入了新的功能 此商城比较简洁,但功能完整。 1.支持产品多图:略图,小图,大图,多图。 2.市场价与热卖价对比。 3.注册与否均可购物。有很多人讨厌注册,这个功能正好迎合了这部分人的使用习惯。 4.除了常见商城的左侧分类树外,还有顶端分类菜单。 5.有友情链接模块。 6.大多功能均制作成独立模块 更新内容: 1.页顶分类菜单——老版本中需要手工修改页面
4
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import pyqtSignal, QObject
import threading
class Worker(QObject):
htmlChanged = pyqtSignal(str)
def execute(self, html):
threading.Thread(target=self.task, daemon=True, args=([html])).start()
def task(self, html):
self.htmlChanged.emit(html)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def update_content(self):
html = self.loadPage2()
self.worker.execute(html)
def initUI(self):
self.worker = Worker(self)
vbox = QVBoxLayout(self)
self.btn = QPushButton()
self.btn.setText("update")
self.btn.clicked.connect(self.update_content)
self.webEngineView = QWebEngineView()
self.webEngineView.setHtml(self.loadPage())
self.worker.htmlChanged.connect(self.webEngineView.setHtml)
vbox.addWidget(self.btn)
vbox.addWidget(self.webEngineView)
self.setLayout(vbox)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('QWebEngineView')
self.show()
def loadPage(self):
with open('test.html', 'r') as f:
html = f.read()
return html
def loadPage2(self):
with open('test2.html', 'r') as f:
html = f.read()
return html
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()确保存在 test.html 和 test2.html 两个文件,内容如下:
test.html:
<html lang="en"><head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>This is a test 1.</p>
</body></html>test2.html:
<html lang="en"><head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>This is a test 2.</p>
</body></html>运行上述代码,你会看到一个窗口,其中包含一个 QWebEngineView 组件和一个按钮。初始时,QWebEngineView 显示 test.html 的内容。点击按钮后,QWebEngineView 会异步更新为 test2.html 的内容,而不会阻塞主线程,从而保证程序的流畅性。
通过使用信号与槽机制和多线程,我们可以安全、高效地更新 QWebEngineView 的 HTML 内容,避免阻塞主线程,从而提高应用程序的响应速度和用户体验。这种方法适用于需要动态更新 Web 内容的各种 PyQt5 应用。
以上就是使用 PyQt5 的 QWebEngineView 更新 HTML 内容的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号