Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWebKit linkClicked signal never fires

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import QWebView

app = QApplication(sys.argv)
web_view = QWebView()
def url_changed(url):  print 'url changed: ', url
def link_clicked(url):  print 'link clicked: ', url
def load_started():  print 'load started'
def load_finished(ok):  print 'load finished, ok: ', ok
web_view.connect(web_view, SIGNAL("urlChanged(const QUrl&)"), url_changed)
web_view.connect(web_view, SIGNAL("linkClicked(const QUrl&)"), link_clicked)
web_view.connect(web_view, SIGNAL('loadStarted()'), load_started)
web_view.connect(web_view, SIGNAL('loadFinished(bool)'), load_finished)
web_view.load(QUrl('http://google.com'))
web_view.show()
sys.exit(app.exec_())

The linkClicked signal isn't working. The other signals work. Qt 4.6.2 on Win XP.

like image 826
Jesse Aldridge Avatar asked Nov 24 '25 12:11

Jesse Aldridge


1 Answers

The link delegation policy must be set appropriately for the linkClicked signal to be emitted.

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import QWebPage, QWebView

app = QApplication(sys.argv)
web_view = QWebView()
web_view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
def url_changed(url):  print 'url changed: ', url
def link_clicked(url):  print 'link clicked: ', url
def load_started():  print 'load started'
def load_finished(ok):  print 'load finished, ok: ', ok
web_view.connect(web_view, SIGNAL("urlChanged(const QUrl&)"), url_changed)
web_view.connect(web_view, SIGNAL("linkClicked(const QUrl&)"), link_clicked)
web_view.connect(web_view, SIGNAL('loadStarted()'), load_started)
web_view.connect(web_view, SIGNAL('loadFinished(bool)'), load_finished)
web_view.load(QUrl('http://google.com'))
web_view.show()
sys.exit(app.exec_())
like image 172
Judge Maygarden Avatar answered Nov 28 '25 18:11

Judge Maygarden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!