您在演示浏览器示例中看到这是如何完成的吗?
QWebEnginePage *WebPage::createWindow(QWebEnginePage::WebWindowType type)
{
if (type == QWebEnginePage::WebBrowserTab) {
return mainWindow()->tabWidget()->newTab()->page();
} else if (type == QWebEnginePage::WebBrowserWindow) {
BrowserApplication::instance()->newMainWindow();
BrowserMainWindow *mainWindow = BrowserApplication::instance()->mainWindow();
return mainWindow->currentTab()->page();
} else {
PopupWindow *popup = new PopupWindow(profile());
popup->setAttribute(Qt::WA_DeleteOnClose);
popup->show();
return popup->page();
}
}
如果你仍然想委派这项工作,通知主窗口/应用程序/whatever,你可能可以拦截点击和存储链接,但我不确定调用顺序,另外你必须注意请求窗口只是一个“新标签”(没有 url 的空标签):
bool WebPage::acceptNavigationRequest(const QUrl & url, NavigationType type, bool isMainFrame)
{
switch( type )
{
case QWebEnginePage::NavigationTypeLinkClicked:
{
mLastClickedLink = url; //-- clear it in WebPage::createWindow
return true;
}
default:
return QWebEnginePage::acceptNavigationRequest( url, type, isMainFrame );
}
}