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?

Thanks, Albert


That does it:

import sys, webbrowser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)
web = QWebView()


def linkClicked(url):
web.connect(web, SIGNAL("linkClicked (const QUrl&)"), linkClicked)


Updated example for PyQt5 (the magic is to re-implement the "acceptNavigationRequest" method):

from PyQt5 import QtWidgets, QtCore, QtGui, QtWebEngineWidgets

class RestrictedQWebEnginePage(QtWebEngineWidgets.QWebEnginePage):
    """ Filters links so that users cannot just navigate to any page on the web,
    but just to those pages, that are listed in allowed_pages.
    This is achieved by re-implementing acceptNavigationRequest.
    The latter could also be adapted to accept, e.g. URLs within a domain."""

    def __init__(self, parent=None):
        self.allowed_pages = []

    def acceptNavigationRequest(self, qurl, navtype, mainframe):
        # print("Navigation Request intercepted:", qurl)
        if qurl in self.allowed_pages:  # open in QWebEngineView
            return True
        else:  # delegate link to default browser
            return False

class RestrictedWebViewWidget(QtWidgets.QWidget):
    """A QWebEngineView is required to display a QWebEnginePage."""

    def __init__(self, parent=None, url=None, html_file=None):
        self.view = QtWebEngineWidgets.QWebEngineView() = RestrictedQWebEnginePage()

        if html_file:
            print("Loading File:", html_file)
            self.url = QtCore.QUrl.fromLocalFile(html_file)
        elif url:
            print("Loading URL:", url)
            self.url = QtCore.QUrl(url)

        # associate page with view

        # set layout
        self.vl = QtWidgets.QVBoxLayout()

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    web = RestrictedWebViewWidget(url="YOUR URL")  # or YOUR local HTML file

When you click a link that has the target="_blank" attribute, QT calls the CreateWindow method in QWebEnginePage to create a new tab/new window.

The key is to re-implement this method to, instead of opening a new tab, open a new browser window.

class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
    def createWindow(self, _type):
        page = WebEnginePage(self)
        return page

    def open_browser(self, url):
        page = self.sender()

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.url = QUrl("")
        self.webView = QWebEngineView() = WebEnginePage(self.webView)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    web = MainWindow()

Need Your Help

You cannot use this application with this Mac OS version

macos terminal osx-yosemite yosemite

My Mac OS X version is 10.10.1 Yosemite. I wanted to install Java 7 on my Mac. So when I try to install it says "you need Mac OS X version 10.7.3 and later". So I found the following from the web to

UIActivityIndicatorView Trouble

iphone uiactivityindicatorview

This is a bit of a silly question but I don't know how to get around it.