代码之家  ›  专栏  ›  技术社区  ›  Albert

在qtwebview中捕获链接单击并在默认浏览器中打开

  •  7
  • Albert  · 技术社区  · 15 年前

    我正在qtwebview中打开一个页面(在pyqt中,如果这很重要),我想在系统默认浏览器中打开所有链接。也就是说,单击链接不应更改qtwebview中的站点,但应使用默认浏览器打开它。我想让用户不可能在qtwebview中更改站点。

    我该怎么做?

    谢谢, 艾伯特

    2 回复  |  直到 7 年前
        1
  •  9
  •   Albert    15 年前

    就是这样:

    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_())
    
        2
  •  0
  •   bst    7 年前

    更新了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_())