import sys
from PySide import QtGui, QtWebKit, QtCore
class WebPageProcessor(object):
def __init__(self, browser):
self.browser = browser
self.browser.loadFinished.connect(self.test)
self.browser.load('http://google.com')
def test(self, ok):
print(ok)
class Browser(QtGui.QWidget):
def __init__(self):
super().__init__()
self.init_ui()
bot = WebPageProcessor(self.browser)
def init_ui(self):
self.browser = QtWebKit.QWebView()
grid = QtGui.QGridLayout()
grid.setContentsMargins(0, 0, 0, 0)
grid.addWidget(self.browser, 0, 0)
self.setLayout(grid)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
browser = Browser()
browser.show()
app.exec_()
'Browser' class is the UI browser and 'WebPageProcessor' is the class which controls it.
I have binded 'loadFinished' signal to the browser but it's never called.
If I resign from 'WebPageProcessor' class and put all it's code into 'init_ui' method, then the signal is emitted correctly.
What's the reason and how to fix it?
The loadFinished
certainly is emitted. But the test
slot will never be called, because it gets deleted immediately after it is connected. This is because you're allowing the instance of WebPageProcessor
to be garbage-collected once __init__
has returned.
Try this instead:
self.bot = WebPageProcessor(self.browser)