Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. PyQt get a link's "src" attribute when it is clicked on in a QtWebView
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved Qt WebKit
4 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jspeirs
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jspeirs
      wrote on last edited by
      #2

      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))
      
      1 Reply Last reply
      0
      • K Offline
        K Offline
        Konstantin Tokarev
        wrote on last edited by
        #3

        Use setLinkDelegationPolicy and linkClicked signal in QWebPage

        1 Reply Last reply
        2
        • J Offline
          J Offline
          jspeirs
          wrote on last edited by
          #4

          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())
          
          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved