Search code examples
pythonqtpyqtqtwebkit

QtWebkit: loadFinished and loadProgress slots are never executed


Here is a basic PyQt code for Webkit I was trying out.

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *

class XPrinter(QObject):

    def __init__(self):
        QObject.__init__(self)

    def print_page_info(self, ok):
        print ok

    def print_load_started(self):
        print 'started loading'

    def print_load_percent(self, percent):
        print percent


app = QApplication(sys.argv)

web = QWebView()
xprinter = XPrinter()
QObject.connect(web, SIGNAL("loadFinished()"), xprinter.print_page_info)
QObject.connect(web, SIGNAL("loadStarted()"), xprinter.print_load_started)
QObject.connect(web, SIGNAL("loadProgress()"), xprinter.print_load_percent)
web.load(QUrl("http://www.gnu.org"))
web.setWindowState(Qt.WindowMaximized)
web.show()

sys.exit(app.exec_())

I am facing the problem that the slots loadFinished and loadProgress are never executed. Please tell me where I am doing it wrong?


Solution

  • Try using the new style signals

    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from PyQt4.QtWebKit import *
    from PyQt4.QtNetwork import *
    
    class XPrinter(QObject):
    
        def __init__(self):
            QObject.__init__(self)
    
        def print_page_info(self, ok):
            print ok
    
        def print_load_started(self):
            print 'started loading'
    
        def print_load_percent(self, percent):
            print percent
    
    
    app = QApplication(sys.argv)
    
    web = QWebView()
    xprinter = XPrinter()
    web.loadFinished.connect(xprinter.print_page_info)
    web.loadStarted.connect(xprinter.print_load_started)
    web.loadProgress.connect(xprinter.print_load_percent)
    
    web.load(QUrl("http://www.gnu.org"))
    web.setWindowState(Qt.WindowMaximized)
    web.show()
    
    sys.exit(app.exec_())
    

    EDIT: Also you had the wrong signatures QWebview

    QObject.connect(web, SIGNAL("loadFinished(bool)"), xprinter.print_page_info)
    QObject.connect(web, SIGNAL("loadStarted()"), xprinter.print_load_started)
    QObject.connect(web, SIGNAL("loadProgress(int )"), xprinter.print_load_percent)