[英]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.