QWebView does not open links in a new window and does not launch an external pdf processing application

I am using QWebView in this way:

QWebView *window = new QWebView();
window->setUrl(QString("my url"));
window->show();

And it works. I see the html page I want. The problem is this. By default, if you "right-click" the link, the "Open in a new window" action will be shown, but if I click on it, nothing will happen. If I "left click" on the same link, it works. Therefore, the problem is that QWebView does not open new windows. Does anyone know why?

I have another problem. Some links are a pdf file, so I expect QWebView to ask me to download it or run an application to open it. But instead, nothing happens. I think the problem is that no new windows can be opened by QWebView, not PDF.

Obviously, I tested the page using a web browser, and everything works well, so the problem is with some QWebView settings.

Does anyone know how to get QWebView to open new windows as needed?

Notes:

  • all links are local resources.

  • Html links use this syntax (and they work):

 <a href="./something.htm" TARGET="_parent">Some link</a>
  • The pdfs link uses this syntax (nothing happens when I click):
<a href="./pdf/mydoc.pdf" TARGET="pdfwin">Some pdf</a>
+5
2

. , . , .

    QWebView *window = new QWebView();
    window->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);//Handle link clicks by yourself
    window->page()->setContextMenuPolicy(Qt::NoContextMenu); //No context menu is allowed if you don't need it
    connect( window, SIGNAL( linkClicked( QUrl ) ),
                  this, SLOT( linkClickedSlot( QUrl ) ) );

    window->setUrl(QString("my url"));
    window->show();

    //This slot handles all clicks    
    void MyWindow::linkClickedSlot( QUrl url )
    {
        if (url.ishtml()//isHtml does not exist actually you need to write something like it by yourself
             window->load (url);
        else//non html (pdf) pages will be opened with default application
            QDesktopServices::openUrl( url );
    }

, HTML / , QWebPage::DelegateExternalLinks QWebPage::DelegateAllLinks.

+7

, . QWebPage:: action (OpenLinkInNewWindow) QWebPage:: triggerAction .

+1

All Articles