简体   繁体   中英

QWebEngineView: showing load progress

I'm using a QWebEngineView widget with PyQt5 and Python 3.6. I want to show progress, when a page is loading. For demonstration purposes I just do this using print statements (later I want to let a progress bar widget appear, show the progress and disappear when done).

I connected to the events loadStarted, loadProgress, loadFinished.

Code looks like this ( self.browser is the QWebEngineView widget):

def loadStartedHandler(self):
    print(time.time(), ": load started")

def loadProgressHandler(self, prog):
    print(time.time(), ":load progress", prog)

def loadFinishedHandler(self):
    print(time.time(), ": load finished")

# ....

self.browser.loadStarted.connect(lambda: self.loadStartedHandler())
self.browser.loadProgress.connect(lambda: self.loadProgressHandler(42))
self.browser.loadFinished.connect(lambda: self.loadFinishedHandler())

Now, of course, instead of the number 42 I would like to get the actual progress value of the loadProgress event. How do I get this?

Sorry, if this question is dumb. I'm a newbee and I just can't translate it to Python from the C++ documentation. (Unfortunately, I can't find decent Python docu for Qt.)

It is not necessary to pass a lambda, you can connect it directly using the new connection syntax :

def loadStartedHandler(self):
    print(time.time(), ": load started")

def loadProgressHandler(self, prog):
    print(time.time(), ":load progress", prog)

def loadFinishedHandler(self):
    print(time.time(), ": load finished")

   ....

    self.browser.loadStarted.connect(self.loadStartedHandler)
    self.browser.loadProgress.connect(self.loadProgressHandler)
    self.browser.loadFinished.connect(self.loadFinishedHandler)

Example:

import sys
import time

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        lay = QtWidgets.QVBoxLayout(self)
        self.browser = QtWebEngineWidgets.QWebEngineView()
        lay.addWidget(self.browser)
        self.browser.setUrl(QtCore.QUrl("https://www.google.com"))
        self.browser.loadStarted.connect(self.loadStartedHandler)
        self.browser.loadProgress.connect(self.loadProgressHandler)
        self.browser.loadFinished.connect(self.loadFinishedHandler)

    @QtCore.pyqtSlot()
    def loadStartedHandler(self):
        print(time.time(), ": load started")

    @QtCore.pyqtSlot(int)
    def loadProgressHandler(self, prog):
        print(time.time(), ":load progress", prog)

    @QtCore.pyqtSlot()
    def loadFinishedHandler(self):
        print(time.time(), ": load finished")

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    view = Widget()
    view.show()
    sys.exit(app.exec_())

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