开发者

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()

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


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):
        super().__init__(parent)
        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
            QtGui.QDesktopServices.openUrl(qurl)
            return False


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

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

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

        # associate page with view
        self.view.setPage(self.page)

        # set layout
        self.vl = QtWidgets.QVBoxLayout()
        self.vl.addWidget(self.view)
        self.setLayout(self.vl)


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


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)
        page.urlChanged.connect(self.open_browser)
        return page

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

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

        self.url = QUrl("https://stackoverflow.com/")
        self.webView = QWebEngineView()
        self.page = WebEnginePage(self.webView)
        self.webView.setPage(self.page)
        self.webView.load(self.url)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜