How to get link URL on onClick in a QWebView?

与世无争的帅哥 提交于 2019-12-11 02:19:23

问题


How do I get a link's URL when clicking on the link?

If I have a link in the A tag, it's simple: just connect linkClicked(const QUrl&) signal to specific slot.

But if I have a table with an "onClick" event on its cell (generated html: "<td onClick=\"window.location.href='" + link_ + "';\" ......blahblahblah"), it's not working. Why?


回答1:


As it's name suggests, the linkClicked signal is only emitted whenever a link is activated.

But you can intercept all navigation requests by reimplementing acceptNavigationRequest:

class WebPage(QtWebKit.QWebPage):
    def __init__(self, parent=None):
        super(WebPage, self).__init__(parent)
        self.setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateExternalLinks)

    def acceptNavigationRequest(self, frame, request, type):
        print('Navigation Request:', request.url())
        return False

...

webview.setPage(WebPage())

But note that all navigation requests are passed through this method, so your implementation should return True whenever it does not intend to handle the request.



来源:https://stackoverflow.com/questions/27604858/how-to-get-link-url-on-onclick-in-a-qwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!