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. [SOLVED] QNetworkReply - best practices
QtWS25 Last Chance

[SOLVED] QNetworkReply - best practices

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.2k Views
  • 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Currently, I am using the SIGNAL readyRead() on my QNetworkReply object. I realized that sometimes there is still more data and readyRead() is called multiple time (for example when receiving a big xml file from my web service)

    Is there a difference from using the SIGNAL finished() on the QNetworkReply object? For me it would be easier, since I know all the data is ready with finished(), with readyRead(), how do I know if there is more data coming? Also, is using finished() bad practice, I may have to read a lot of xml data, would readyRead() be better for performance? as it read data when is it available? If so, my main question is how to know when the data transfer is complete, do I have to check if finished() has been triggered each loop of readyRead() or is there an easier way, thank you!

    Code sample :

    @void Main_WorkoutPage::on_pushButton_checkDB_clicked() {

    const QString url = "---- (erased for privacy)";
    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    
    replyGetWorkoutList = manager->get(request);
    connect(replyGetWorkoutList, SIGNAL(readyRead()), this, SLOT(slotReadyReadWorkoutList()));
    connect(replyGetWorkoutList, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotErrorWorkoutList(QNetworkReply::NetworkError)));
    connect(replyGetWorkoutList, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrorsWorkoutList(QList<QSslError>)));
    connect(replyGetWorkoutList, SIGNAL(finished()), this, SLOT(testSlotFinished()) ) ;
    

    }

    void Main_WorkoutPage::testSlotFinished() {

    qDebug() << "OK SLOT FINISHED!";
    QByteArray arrayData =  replyGetWorkoutList->readAll();
    qDebug() << arrayData;
    

    }
    @


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      [quote]is using finished() bad practice, I may have to read a lot of xml data, would readyRead() be better for performance?[/quote]
      I guess it depends on how much data you are downloading. If the data is small enough to fit in your RAM, connecting to QNetworkReply::finished() and then calling QNetworkReply::readAll() is a good solution.

      With XML data though, you can attach your QNetworkReply to a QXmlStreamReader. The reader can handle data that arrives in multiple chunks.

      [quote]with readyRead(), how do I know if there is more data coming?[/quote]

      • http://qt-project.org/doc/qt-5/qnetworkreply.html#isFinished
      • http://qt-project.org/doc/qt-5/qnetworkreply.html#isRunning

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        Here is an example of xml file I get, this one has 5 intervals
        http://maximumtrainer.com/phprestsql/?username=max3&password=123&getListWorkoutForUser=1

        I guess this one is small enought to fit in the RAM, i'll have to check with bigger file later, it could grow to 200-1000 intervals, maybe writing the xml file to disk with readyRead() or another solution could be better when this arrive, thanks!


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #4

          Just a note, I found the perfect solution in "qt doc":http://qt-project.org/doc/qt-5.0/qtcore/qxmlstreamreader.html#details

          bq. Incremental parsing
          QXmlStreamReader is an incremental parser. It can handle the case where the document can't be parsed all at once because it arrives in chunks (e.g. from multiple files, or over a network connection). When the reader runs out of data before the complete document has been parsed, it reports a PrematureEndOfDocumentError. When more data arrives, either because of a call to addData() or because more data is available through the network device(), the reader recovers from the PrematureEndOfDocumentError error and continues parsing the new data with the next call to readNext().
          For example, if your application reads data from the network using a network access manager, you would issue a network request to the manager and receive a network reply in return. Since a QNetworkReply is a QIODevice, you connect its readyRead() signal to a custom slot, e.g. slotReadyRead() in the code snippet shown in the discussion for QNetworkAccessManager. In this slot, you read all available data with readAll() and pass it to the XML stream reader using addData(). Then you call your custom parsing function that reads the XML events from the reader.


          Free Indoor Cycling Software - https://maximumtrainer.com

          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