PyQt get a link's "src" attribute when it is clicked on in a QtWebView



  • In PyQt is there a way to capture the src attribute on an anchor (link) when it is clicked on?

    I guess ultimately I'm looking for an event listener for whenever a link is clicked on a webpage that's in a QtWebView.



  • This answer looks promising but I'm not sure how to implement it in my code:

    webview = QtWebKitWidgets.QWebView()  
    webview.setWindowTitle(title)
    webpage = webview.page()        
    webpage.setNetworkAccessManager(self.WebNetworkAccessManager)   
    webview.load(QUrl(url))
    


  • Use setLinkDelegationPolicy and linkClicked signal in QWebPage



  • Ah cool, got it. Ended up going with something like this. Thanks

    webview = WebBrowserView()  
    webview.setWindowTitle(title)
    webpage = webview.page()        
    webpage.setNetworkAccessManager(self.WebNetworkAccessManager)  
    webpage.setLinkDelegationPolicy(QtWebKitWidgets.QWebPage.DelegateExternalLinks)
     
    webview.load(QUrl(url))
    
    class WebBrowserView(QtWebKitWidgets.QWebView):
        def __init__(self, parent=None):
            super(WebBrowserView, self).__init__()
            self.linkClicked.connect(self.handleLinkClicked)
    
        def handleLinkClicked(self, url):
            print(url.toString())
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.