Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAcdessManager not working as expected
Qt 6.11 is out! See what's new in the release blog

QNetworkAcdessManager not working as expected

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 667 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by
    #1

    Hi guys :)
    I wrote a little wrapper I called "ControlledDownloader" to download web pages. The class' constructor takes as argument the function which is going to be called when the download finishes.

    Here is the class:

    class ControlledDownloader  {
    
       QNetworkAccessManager nam;
       QNetworkRequest req;
       std::function< void( QNetworkReply* ) > toCall;
    
    public:
    
       ControlledDownloader(const std::function<void(QNetworkReply*)>& _toCall) : toCall(_toCall){
    
            QObject::connect( &nam, &QNetworkAccessManager::finished, [&]( QNetworkReply * reply ){ toCall(reply); } );
    
       }
    
       void getPage( QString url ){
           nam.get( QNetworkRequest(url) );
       }
    
    };
    

    Apparently, when doing something like this:

    ControlledDownloader * c = new ControlledDownloader([](QNetworkReply * reply){qDebug()<<QString(reply->readAll()).split("\n").size();});
    c->getPage("https://forum.qt.io/");
    

    the code works just expected, printing the number of lines in the source code of a page.

    However, I thought this happens in the "background" (asynchronous, if I'm right) so I tried doing something like this:

    ControlledDownloader * c = new ControlledDownloader([](QNetworkReply * reply){qDebug()<<QString(reply->readAll()).split("\n").size();});
       qDebug()<<"start";
       c->getPage("https://forum.qt.io/");
       fibbo(46);
       qDebug()<<"ok";
    

    Fibbo is a recursive implementation of the Fibonacci algorithm, on my system fibbo(46) takes some good seconds to run.
    I expected the following output:

    start
    623 (no. of lines in source code)
    ok
    

    , since the computation of fibbo(46) takes much longer than the download of the qt forum webpage. However, this is the output I get:

    start
    ok
    623
    

    , which means that the actual download begins after fibbo function ends. Therefore, I'm pretty sure I'm missing something important. Could you tell me where is the problem ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Your fibbo function blocks the main event loop thus breaks the asynchronous nature of Qt. Either move the execution of fibbo to another thread or move the network handling to another thread however fibbo will still block the main thread.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

        Thanks @SGaist . I'll look into threads.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved