从 QWebView 打开外部浏览器

发布于 2024-07-17 12:38:23 字数 161 浏览 2 评论 0原文

我的应用程序上有一个“起始页”,它只是一个从我的服务器下载页面的 QWebView 小部件。

当用户单击链接时,我希望它在默认浏览器中打开,而不是在小部件中打开。

有没有办法用 QWebView 来做到这一点? 或者我应该采取不同的方法?

I have a "start page" on my application that is just a QWebView widget that downloads a page from my server.

When the user clicks on a link I'd like it to open in their default browser, not in the widget.

Is there a way to do this with QWebView? Or should I take a different approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-07-24 12:38:23

只需调用 QWebPage::setLinkDelegationPolicy(QWebPage::DelegateAllLinks) 然后将信号 QWebView::linkClicked (const QUrl & url) 连接到插槽,并在插槽中执行浏览器。
找出默认浏览器是什么稍微复杂一些,并且取决于用户运行的桌面。 环境变量 BROWSER 通常包含默认值。

或者使用 QDesktopServices::openUrl [如 gnud 在评论中建议的那样]

Just call QWebPage::setLinkDelegationPolicy(QWebPage::DelegateAllLinks) then connect the signal QWebView::linkClicked (const QUrl & url) to a slot, and in the slot execute the browser.
Finding out what the default browser is, is slightly more complicated and depends on which desktop the user is running. The environment variable BROWSER often contains the default.

Or use QDesktopServices::openUrl [as suggested by gnud in a comment]

辞别 2024-07-24 12:38:23

假设链接是外部的,您只需调用 QWebView::setOpenExternalLinks(true) 在你的 QWebView 小部件上。 该属性默认为 false; 将其设置为 true 会导致外部链接在您的默认 Web 浏览器中打开。

Assuming the links are external, you can just call QWebView::setOpenExternalLinks(true) on your QWebView widget. The property is false by default; setting it to true causes external links to open in your default web browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文