views:

479

answers:

1

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?

+1  A: 

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

serge_gubenko
This only works on HTML pages though? What if I click on some other type of link, say an mp3? How can I get the MIME type in that case?
pafcu
pls, see the update for my original reply
serge_gubenko