QWebView获取内容类型

发布于 2024-08-17 12:39:25 字数 101 浏览 3 评论 0原文

当我单击 QWebView 中的链接时,我需要根据该链接的 MIME 类型执行一些操作。

显然,当我单击链接 QWebView 尝试导航到那里时,但是之后如何获取内容类型?

When I click on a link in a QWebView I need to perform some actions based on the MIME type of that link.

Obviously when I click on the link QWebView tries to navigate there, but how do I get the content type after this?

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

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

发布评论

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

评论(1

悲凉≈ 2024-08-24 12:39:25

我相信您可以在页面加载后通过查看 QWebElment 的集合来获取加载页面的内容类型。首先,您需要为 webview 小部件设置 loadFinished 事件槽。像这样:

QWebView::connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(on_pageLoad_finished(bool)));

每次将页面加载到 webview 控件中时,都应该执行 on_pageLoad_finished 方法。在那里,您可以通过 webframe 对象访问 Web 元素集合。下面是 on_pageLoad_finished 插槽的可能实现,

void MainWindow::on_pageLoad_finished(bool ok)
{
    if (!ok) return;

    QWebFrame* frame = ui->webView->page()->currentFrame();
    if (frame!=NULL)
    {
        QWebElementCollection collection = frame->findAllElements("meta[http-equiv=content-type]");
        foreach (QWebElement element, collection)
        {
            qDebug() << element.attribute("http-equiv");
            qDebug() << element.attribute("content");
        }
    }
}

它应该为加载到应用程序输出中的每个新页面转储内容类型。

update0 图像链接:

如果用户点击图像或者您只是像这样加载它,则下划线 DOM 模型中没有带有 content-type 属性的元元素:

QUrl url("http://www.motociclismo.es/rcs/noticias/2008/10_Oct/0610-bmw-s1000RR-02.jpg");
ui->webView->load(url);

但仍然创建了一些 dom 对象。对于这个特定的图像,它应该如下图所示(您可以通过frame->toHtml 获得)。

<html>
<body style="margin: 0px;">
<img style="-webkit-user-select: none; cursor: -webkit-zoom-in; " src="http://www.motociclismo.es/rcs/noticias/2008/10_Oct/0610-bmw-s1000RR-02.jpg" width="426" height="320">
</body>
</html>

因此,如果您使用下面的代码查询 IMG 对象:

QWebElementCollection collection0 = frame->findAllElements("img");
foreach (QWebElement element, collection0)
{
    QStringList attributesList = element.attributeNames();
    foreach (QString attributeName, attributesList)
    {
        qDebug() << attributeName << ":" << element.attribute(attributeName);
    }
}

您应该得到以下结果:

"style" : "-webkit-user-select: none; cursor: -webkit-zoom-in; "  
"src" : "http://www.motociclismo.es/rcs/noticias/2008/10_Oct/0610-bmw-s1000RR-02.jpg"
"width" : "426"  
"height" : "320"

src 属性为您提供所显示图像的链接。我想你可以在检测类型时信任它的扩展名;或者只是下载图像并使用文件签名检测其类型。您可以为此编写代码或使用一些第三方映像库来获取文件类型。

希望这有帮助,问候

I believe you can get the content type of the loaded page by looking into QWebElment's collection after your page is loaded. First you need to set up loadFinished event slot for the webview widget. Like this:

QWebView::connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(on_pageLoad_finished(bool)));

on_pageLoad_finished method should be executed every time a page is loaded into webview control. There you have access to web elements collections through the webframe object. Below is a possible implementation of the on_pageLoad_finished slot

void MainWindow::on_pageLoad_finished(bool ok)
{
    if (!ok) return;

    QWebFrame* frame = ui->webView->page()->currentFrame();
    if (frame!=NULL)
    {
        QWebElementCollection collection = frame->findAllElements("meta[http-equiv=content-type]");
        foreach (QWebElement element, collection)
        {
            qDebug() << element.attribute("http-equiv");
            qDebug() << element.attribute("content");
        }
    }
}

it should dump content type for each new page loaded into the application output.

update0 image links:

there is no meta element with content-type attribute in the underline DOM model if user clicks on the image or if you would just load it like that:

QUrl url("http://www.motociclismo.es/rcs/noticias/2008/10_Oct/0610-bmw-s1000RR-02.jpg");
ui->webView->load(url);

but there are still some dom objects created. For this particular image it should look like the one below (you can get via frame->toHtml).

<html>
<body style="margin: 0px;">
<img style="-webkit-user-select: none; cursor: -webkit-zoom-in; " src="http://www.motociclismo.es/rcs/noticias/2008/10_Oct/0610-bmw-s1000RR-02.jpg" width="426" height="320">
</body>
</html>

so if you would query for IMG objects by using code below:

QWebElementCollection collection0 = frame->findAllElements("img");
foreach (QWebElement element, collection0)
{
    QStringList attributesList = element.attributeNames();
    foreach (QString attributeName, attributesList)
    {
        qDebug() << attributeName << ":" << element.attribute(attributeName);
    }
}

you should be getting following result:

"style" : "-webkit-user-select: none; cursor: -webkit-zoom-in; "  
"src" : "http://www.motociclismo.es/rcs/noticias/2008/10_Oct/0610-bmw-s1000RR-02.jpg"
"width" : "426"  
"height" : "320"

src attribute gives you the link for the image shown. I guess you can either trust its extension when detecting the type; or just download the image and detect it's type by using file signature. You can write the code for this or use some 3rd party imaging libraries to get the file type.

hope this helps, regards

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