4

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
petro4213
  • 163
  • 9

1 Answers1

5

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • @eyllanesc Can the "loadProgressHandler()" function take any additional arguments? To be honest - I already tried this with no success...so I am trying to find the correct way of doing this (if it's even possible). – SilSur Jun 04 '19 at 07:00
  • @eyllanesc I want to provide 3 external args that are inputs to a secondary function which is only executed when prog == 100 !! I don't know how else to explain it - the MRE I have is not minimal (short) enough for me to post here. – SilSur Jun 04 '19 at 07:10
  • @eyllanesc I just refactored the code and your class member suggestion works 100% !! I don't know why I didn't see this logic earlier?! Too many classes I guess... Thanks anyway. – SilSur Jun 04 '19 at 07:35