如何在QWebView的onClick上获取链接URL? [英] How to get link URL on onClick in a QWebView?

查看:229
本文介绍了如何在QWebView的onClick上获取链接URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击链接时如何获得链接的URL?

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

如果我在A标签中有一个链接,那很简单:只需将linkClicked(const QUrl&)信号连接到特定插槽即可.

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

但是,如果我有一个单元格上带有"onClick"事件的表(生成的HTML:"<td onClick=\"window.location.href='" + link_ + "';\" ......等等……为什么),为什么?

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?

推荐答案

顾名思义, linkClicked 信号仅在激活 link 时发出.

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

但是您可以通过重新实现 acceptNavigationRequest 来拦截所有导航请求:

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

但是请注意,所有导航请求都是通过此方法传递的,因此您的实现只要不打算处理该请求,就应该返回True.

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.

这篇关于如何在QWebView的onClick上获取链接URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆