QWebView 不会在新窗口中打开链接,也不会启动外部应用程序来处理 pdf

发布于 2024-11-27 22:21:24 字数 847 浏览 2 评论 0原文

我以这种方式使用 QWebView:

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

它有效。我可以看到我想要的html页面。 问题是这样的。默认情况下,如果我在链接上“右键单击”,则会显示“在新窗口中打开”操作,但如果我单击它,则不会发生任何情况。如果我在同一个链接上“左键单击”,它就会起作用。 所以问题是QWebView没有打开新窗口。有谁知道为什么?

我还有另一个问题。有些链接是 pdf 文件,所以我希望 QWebView 要求我下载它或运行应用程序来打开它。但什么也没发生。我认为问题与 QWebView 不允许打开新窗口以及 pdf 上不允许打开新窗口有关。

显然,我使用网络浏览器测试了该页面,一切正常,因此问题出在 QWebView 的某些设置中。

有谁知道如何让 QWebView 在需要时打开新窗口?

注意:

  • 所有链接都是本地资源。

  • html 链接使用此语法(并且它们有效):

 一些链接
  • pdf 的链接使用以下语法(单击时没有任何反应):
一些 pdf

I am using a QWebView in this way:

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

And it works. I can see the html page I want.
The problem is this. By default if I "right click" on a link the action "Open in new window" is shown but if I click on it, nothing happens. If I "left click" on the same link it works.
So the problem is that no new windows are open by QWebView. Does anyone know why?

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

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

Does anyone know how to make QWebView open new windows when required?

Notes:

  • all links are local resources.

  • The html links use this syntax (and they works):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫以 2024-12-04 22:21:24

尝试自己处理蜱虫。这是一个可以指导您的示例。我还没有编译它。

    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。

Try to handle cicks by yourself. Here is an example that can guide you. I have not compiled it though .

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

Note that if the HTML you are displaying may containing relative/internal links to other parts of itself, then you should use QWebPage::DelegateExternalLinks instead of QWebPage::DelegateAllLinks.

漫雪独思 2024-12-04 22:21:24

上面的答案内容丰富,但对于这个问题可能会不知所措。
将信号连接到 QWebPage::action(OpenLinkInNewWindow) 或重写 QWebPage::triggerAction 应该可以解决此问题。

The above answer is informative but might be overwhelmed for this question.
Connecting signals to QWebPage::action(OpenLinkInNewWindow) or overriding QWebPage::triggerAction should solve this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文