qtqt4pyqtqtwebkit

catch link clicks in QtWebView and open in default browser


I am opening a page in QtWebView (in PyQt if that matters) and I want to open all links in the system default browser. I.e. a click on a link should not change the site in the QtWebView but it should open it with the default browser. I want to make it impossible to the user to change the site in the QtWebView.

How can I do that?


Solution

  • That does it:

    import sys, webbrowser
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from PyQt4.QtWebKit import *
    
    app = QApplication(sys.argv)
    web = QWebView()
    
    web.load(QUrl("http://www.az2000.de/projects/javascript-project/"))
    web.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
    
    
    def linkClicked(url): webbrowser.open(str(url.toString()))
    web.connect(web, SIGNAL("linkClicked (const QUrl&)"), linkClicked) 
    
    
    web.show()
    
    sys.exit(app.exec_())