Search code examples
qtqtwebengine

QWebEngineView createWindow


OK bending my brain trying to make sense of QWebEngine.

I understand the concept of implementing virtual functions but I'm unsure how to get the url that the user has clicked being a newTab/newWindow link that a page or view has requested.

QWebEngineView * WebEngineTabView::createWindow(QWebEnginePage::WebWindowType type)
{
// signal Main window for a new view( @URL )
emit requestNewTab(page()->requestedUrl());
}

This is for an educational GPL browser app Any help greatly appreciated


Solution

  • Did you saw how this done in the demobrowser example?

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

    If you still want to delegate this work, notifying the mainwindow/app/whatever, you probably can intercept clicks and store links, but I'm not sure about calls order, plus you have to pay attention for cases when requested window is just a "new tab" (an empty tab without 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 );
        }
    }