简体   繁体   中英

Update html content in QWebEngineView

I can not get this code working and show into the app the new html after the first html load.

Here are the files to make the tests.

Offtopic:

This is an extra text to allow the submit of this question, please do not waste your time reading this.

stackoverflow, can you submit the question please?

My goodness, I have to keep typing because it keeps saying that I should provide more details. I can not figure out what other details are needed here. I am sure that with enough words written here the post will be allowed to be submitted. Not yet, I will keep on writing. More? Why not? I have spend so much time with this problem for an application I am developing that I do not know what else to do.

If you solve this problem, you will make me very happy.

End of offtopic.

example.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()

I expected to load the new test2.html into the interface, but it keeps with the first html load.

The test files are:

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>

Solved!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM