[英]Update html content in QWebEngineView
在第一次加载 html 后,我无法让这段代码正常工作并在应用程序中显示新的 html。
这是进行测试的文件。
无关:
这是允许提交此问题的额外文本,请不要浪费时间阅读此内容。
stackoverflow,你可以提交问题吗?
天哪,我必须继续打字,因为它一直说我应该提供更多细节。 我不知道这里还需要什么其他细节。 我敢肯定,这里写的字够多,帖子就可以投了。 还没有,我会继续写。 更多的? 为什么不? 对于我正在开发的应用程序,我花了很多时间来解决这个问题,以至于我不知道还能做什么。
如果你解决了这个问题,你会让我很开心。
题外话结束。
例子.py
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
import time
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout(self)
self.webEngineView = QWebEngineView()
self.loadPage()
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()
self.webEngineView.setHtml(html)
def loadPage2(self):
with open('test2.html', 'r') as f:
html = f.read()
self.webEngineView.stop()
self.webEngineView.setHtml(html)
self.webEngineView.reload() #this line does not update the view
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
time.sleep(5)
ex.loadPage2() # I expected to make this a refresh into the interface with the new content.
if __name__ == '__main__':
main()
我希望将新的 test2.html 加载到界面中,但它与第一个 html 加载保持一致。
测试文件是:
测试.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>
解决了!
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.