Results 1 to 12 of 12

Thread: [QWebView] Blocking images with exceptions

  1. #1
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [QWebView] Blocking images with exceptions

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QWebView] Blocking images with exceptions

    You can attach to the network access manager used by QtWebKit and filter out requests for images you don't want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWebView] Blocking images with exceptions

    Hello, thank you for your answer !

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QWebView] Blocking images with exceptions

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWebView] Blocking images with exceptions

    Well, I can't really see the relation between QWebView and QNetworkAccessManager.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QWebView] Blocking images with exceptions

    QWebView shows a QWebPage that is served via QNetworkAccessManager. Go to the docs of QWebPage.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWebView] Blocking images with exceptions

    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...

  8. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWebView] Blocking images with exceptions

    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

    Have a look here: http://stackoverflow.com/questions/4...588113#4588113

    HIH

    Joh

  9. The following user says thank you to JohannesMunk for this useful post:

    fab_74 (3rd April 2011)

  10. #9
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWebView] Blocking images with exceptions

    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.

    Qt Code:
    1. void FNavigateur::fOpenSomething {
    2. NAM nam;
    3. page = new QWebPage;
    4. WebViewNavigateur->setPage(page);
    5. page->setNetworkAccessManager(&nam);
    6. page->mainFrame()->load(QUrl("http://google.com"));
    7. }
    To copy to clipboard, switch view to plain text mode 

    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?

  11. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: [QWebView] Blocking images with exceptions

    NAM is out of scope, create it on a heap
    Qt Code:
    1. NAM * nam = new NAM(this);
    2. page = new QWebPage;
    3. WebViewNavigateur->setPage(page);
    4. page->setNetworkAccessManager(nam);
    5. page->mainFrame()->load(QUrl("http://google.com"));
    To copy to clipboard, switch view to plain text mode 
    Using more descriptive names for classes won't hurt as well

  12. #11
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: [QWebView] Blocking images with exceptions

    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.

    Qt Code:
    1. void FNavigateur::fOpenSomething{
    2. NAM* nam = new NAM(this);
    3. page = new QWebPage;
    4. WebViewNavigateur->setPage(page);
    5. page->setNetworkAccessManager(nam);
    6. page->mainFrame()->load(QUrl("http://google.com"));
    7. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by JohannesMunk; 3rd April 2011 at 17:13. Reason: cross posting

  13. The following user says thank you to JohannesMunk for this useful post:

    fab_74 (3rd April 2011)

  14. #12
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QWebView] Blocking images with exceptions

    Thank you everyone
    Problem has been solved, thank you again.

Similar Threads

  1. QWebview not showing images
    By manojmka in forum Qt Programming
    Replies: 27
    Last Post: 17th July 2012, 00:22
  2. Replies: 1
    Last Post: 6th December 2011, 23:44
  3. Get images from a QWebView or QWebPage
    By redneon in forum Qt Programming
    Replies: 6
    Last Post: 21st January 2010, 10:03
  4. QWebView not displaying images
    By andyp in forum Qt Programming
    Replies: 7
    Last Post: 3rd December 2009, 22:35
  5. QWebView shows '?' instead of all images
    By doep in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2009, 11:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.