QWebEnginePage::acceptNavigationRequest() not being triggered on link click

125 Views Asked by At

I am porting from QWebKit (Qt 4.8) to QWebEngine (Qt 5.15). In the application, we set some of our own basic HTML into a view (just some text which contains href links). The link URL is actually just some text which we are using to take a certain action in our desktop application.

In Qt 4.8, we would set the QWebPage::DelegateAllLinks policy and connect the linkClicked() signal to our own C++ class to take the appropriate action in the application.

After reading the Qt porting guide and this post, I created my own QWebEnginePage subclass that overrides acceptNavigationRequest() to emit it's own signal on link click action and set it in my QWebEngineView, exactly as described in the post:

bool MyWebEnginePage::acceptNavigationRequest( QUrl const &url, QWebEnginePage::NavigationType type, bool isMainFrame )
{
    if( type == QWebEnginePage::NavigationTypeLinkClicked )
    {
        emit linkClicked( url );
        return false;
    }
    return QWebEnginePage::acceptNavigationRequest( url, type, isMainFrame );
}

And setting up the view/page:

    m_webView = new QWebEngineView( m_widget );
    m_webView->setPage( new MyWebEnginePage() );
    connect( m_webView->page(), SIGNAL( linkClicked( QUrl const & ) ), this, SLOT( onLinkClicked( QUrl const& ) ) );
    m_webView->setHtml( "<html><body>Some Text and a <li><a href=\"linkAction\">Link</a></li></body>" );

The view/page loads the HTML containing the links after calling setHtml(), but the view goes blank and nothing happens in my application after clicking any link. In the debugger, acceptNavigationRequest() is never triggered on the link click at all! It is only triggered once when loading the HTML via setHtml().

I would like to understand what Qt is doing as soon as the link is clicked. Does the link click cause another QWebEnginePage to be created and try to load the nonsense URL we are trying to intercept?

0

There are 0 best solutions below