PDA

View Full Version : [QWebView] Blocking images with exceptions



fab_74
2nd April 2011, 15:00
Hello everyone :)

I would like to filter images loaded by my browser, and block them, in order to increase speed. But the problem is, there are images I'd like not to be blocked (such as reCaptcha).

I've looked for a while on a lot of forums, documentation, and the only thing I found is this :

QWebSettings::globalSettings()->setAttribute(QWebSettings::AutoLoadImages, false);

But it's blocking every image. So... that's not really what I want. I want to block every image, except those who contains recaptcha, let's say.

Thank you in advance for your further answers.

wysota
3rd April 2011, 01:40
You can attach to the network access manager used by QtWebKit and filter out requests for images you don't want.

fab_74
3rd April 2011, 11:01
Hello, thank you for your answer ! :D

I'm sorry but I didn't really understand... Would you mind to give me an example?

wysota
3rd April 2011, 11:14
What exactly didn't you understand? Do you understand the relationship between QWebView and QNetworkAccessManager? Do you see which methods in QNetworkAccessManager you can override? Which of them could be useful in your case?

fab_74
3rd April 2011, 11:27
Well, I can't really see the relation between QWebView and QNetworkAccessManager.

wysota
3rd April 2011, 11:37
QWebView shows a QWebPage that is served via QNetworkAccessManager. Go to the docs of QWebPage.

fab_74
3rd April 2011, 12:07
If I understood correctly, I have to filter what's requested by the QNetworkAccessManager? I read the doc, but I have no idea how to do that... :(

JohannesMunk
3rd April 2011, 15:17
Although I never used QNetworkAccessManager before I think wysota already pointed out the solution. Look at what functions are virtual (only createRequest), thus can be reimplemented in your custom accessManager.

Then before you load the page you set its accessManager to your own using setNetworkAccessManager (http://doc.qt.nokia.com/latest/qwebpage.html#setNetworkAccessManager)

Have a look here: http://stackoverflow.com/questions/4575245/how-to-tell-qwebpage-not-to-load-specific-type-of-resources/4588113#4588113

HIH

Joh

fab_74
3rd April 2011, 15:54
Hello, thank you for your answer and your link.
I've tried to extend QNetworkAccessManager using Piotr Dobrogost's method, but it's not working.


void FNavigateur::fOpenSomething {
NAM nam;
page = new QWebPage;
WebViewNavigateur->setPage(page);
page->setNetworkAccessManager(&nam);
page->mainFrame()->load(QUrl("http://google.com"));
}

Once this function is called, nothing happens. It doesn't crash, but it just doesn't move. My QProgressBar shows me 10%, and doesn't move.
Maybe I'm doing it wrong?

stampede
3rd April 2011, 16:02
NAM is out of scope, create it on a heap


NAM * nam = new NAM(this);
page = new QWebPage;
WebViewNavigateur->setPage(page);
page->setNetworkAccessManager(nam);
page->mainFrame()->load(QUrl("http://google.com"));

Using more descriptive names for classes won't hurt as well :)

JohannesMunk
3rd April 2011, 16:05
You are creating your networkAccessManager in local scope thus the variable is allocated on the stack. It will be destroyed a soon as your fOpenSomething method finishes. And the actual loading of the webpage will occur only after a return to the event loop I guess. And by that time your NAM is gone. What you have to do, is to allocate your instance on the global heap with new. That allows a longer lifetime.



void FNavigateur::fOpenSomething{
NAM* nam = new NAM(this);
page = new QWebPage;
WebViewNavigateur->setPage(page);
page->setNetworkAccessManager(nam);
page->mainFrame()->load(QUrl("http://google.com"));
}
You also have to set a parent for the NAM so, that it will be deleted at some point.

Edit: Stampede was faster :->

HIH

Joh

fab_74
3rd April 2011, 16:30
Thank you everyone :D
Problem has been solved, thank you again.