2011-03-21 153 views
3
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_()) 

linkClicked信號不起作用。其他信號起作用。 Qt 4.6.2在Win XP上。QWebKit linkClicked信號永遠不會觸發

回答

7

link delegation policy必須正確設置linkClicked發出的信號。

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_()) 
+0

啊,好的。謝謝。 – 2011-03-21 20:31:23

相關問題