Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebView, DelegateExternalLinks and anchors



  • Hi,

    I'm trying to use QWebView to display on-the-fly generated html reports. Their source is generated as valid html/css and the passed to the qwebview by setHtml( content )

    The generated code contains three types of links:

    1. links to anchors within the generated code
    2. external links, that should be opened in a external browser
    3. links with a special scheme to be handled within the application

    Out-of-the-box, type 1 works perfectly, 2 are opened inside the webview and 3 are ignored.

    To handle 2 and 3 I set

    qwebview->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
    

    and connect the linkClicked() signal to aprocessing slot.
    This resolves type 2, but anchors result now in an error about:blank#<anchorID> Error while opening the location, the location is not supported (wording may actually be slightly different). Any idea, how I can keep working anchors while handling the other types by myself?

    QT version: 4.8.6

    //edit: typo



  • You should override QWebPage::acceptNavigationRequest and implement your own navigation policy.



  • So if I inherit from QWebPage and override acceptNavigationRequest, it should simply work like

    qwebview->setPage( myNewWebPage );
    qwebview->setHtml( myContent );
    

    shouldn't it?

    //edit: yes it does. Thank you, it worked perfectly fine.


Log in to reply