在 QtWebView 中捕获链接点击并在默认浏览器中打开

发布于 2024-09-08 12:48:14 字数 147 浏览 10 评论 0原文

我正在 QtWebView 中打开一个页面(如果重要的话,在 PyQt 中),我想打开系统默认浏览器中的所有链接。即,单击链接不应更改 QtWebView 中的站点,但应使用默认浏览器打开它。我想让用户无法更改 QtWebView 中的站点。

我怎样才能做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花落人离去 2024-09-15 12:48:14

这样做就可以了:

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

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_())
一梦等七年七年为一梦 2024-09-15 12:48:14

PyQt5 的更新示例(神奇之处在于重新实现“acceptNavigationRequest”方法):

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

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_())
沫雨熙 2024-09-15 12:48:14

当您单击具有 target="_blank" 属性的链接时,QT 会调用 QWebEnginePage 中的 CreateWindow 方法来创建新选项卡/新窗口。

关键是重新实现此方法,而不是打开新选项卡,而是打开新的浏览器窗口。

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

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_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文