发出 newView Requested 信号时在外部浏览器中打开 URL

Posted

技术标签:

【中文标题】发出 newView Requested 信号时在外部浏览器中打开 URL【英文标题】:Opening an URL in external browser when newViewRequested signal emited 【发布时间】:2017-02-28 09:58:57 【问题描述】:

我使用 WebEngineView QML 类型来显示一个网页,该网页有一些需要在新选项卡中打开的链接。链接类似于

<a href="http://google.com" target="_blank">Go to google in new tab</a>

我想在外部浏览器中打开 newViewRequested signal 的 URL,但 WebEngineNewViewRequest 没有我可以与 Qt.openUrlExternally(request.url) 一起使用的“url”属性。

WebEngineNewViewRequest 有一个私有成员 QUrl m_requestedUrl 不能作为 qml 中的属性访问。 我该如何处理这个问题,获取 URL 并在外部浏览器中打开它。 谢谢。

【问题讨论】:

【参考方案1】:

在Qt5中,可以使用navigationRequested信号来实现:

onNavigationRequested: function(request) 
    if (request.navigationType === WebEngineNavigationRequest.LinkClickedNavigation) 
        Qt.openUrlExternally(request.url)
    
    request.action = WebEngineNavigationRequest.IgnoreRequest

IgnoreRequest 分配给action 属性的行是为了确保URL 没有在WebEngineView 中打开。

【讨论】:

以上是关于发出 newView Requested 信号时在外部浏览器中打开 URL的主要内容,如果未能解决你的问题,请参考以下文章