QWebPage 强制在同一选项卡中打开链接
我正在 Qt 中开发单选项卡浏览器,但遇到了一个问题:
网络上指向外部网站的链接设置为在新选项卡中打开(使用 taget='_blank')。然而,由于我的浏览器只有一个选项卡,这些链接根本处于非活动状态。
我相信可以通过注入 JavaScript (QWebFrame::eveluateJavaScript) 来强制链接在同一选项卡中打开,这似乎是一个低效的解决方案。
Qt 中是否有一种本地方法可以强制所有链接在同一选项卡/窗口中打开?
编辑:
我通过重新实现 loadFinished(bool) 提出了一种本机方法:
void MyBrowser::loadFinished(bool ok) {
if (!ok) {
return;
}
QWebFrame* frame = webView->page()->mainFrame();
if (frame!=NULL) {
QWebElementCollection collection = frame->findAllElements("a[target=_blank]");
foreach (QWebElement element, collection) {
element.setAttribute("target", "_self");
}
}
}
如果有更好的方法,我会很高兴看到它。
I'm working on a one tab browser in Qt and I've encountered a problem:
The links on the web that lead to external websites are set to open in a new tab (using taget='_blank'). However, since my browser has just one tab those links are simply inactive.
I believe that it's possible to force the links to open in the same tab by injecting JavaScript (QWebFrame::eveluateJavaScript), by that seems like an inefficient solution.
Is there a native way in Qt to force all links to open in the same tab/window?
Edit:
I've come up with a native way by re-implementing loadFinished(bool) :
void MyBrowser::loadFinished(bool ok) {
if (!ok) {
return;
}
QWebFrame* frame = webView->page()->mainFrame();
if (frame!=NULL) {
QWebElementCollection collection = frame->findAllElements("a[target=_blank]");
foreach (QWebElement element, collection) {
element.setAttribute("target", "_self");
}
}
}
If there's an even better way I'd be glad to see it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建QWebPage的子类,并重写createWindow()。最简单的方法如下:
并且,将上面的网页设置为 webview,如下所示:
Make QWebPage's subclass, and override createWindow(). Most simple way is as follows:
And, set above webpage to webview like this: