Results 1 to 8 of 8

Thread: Multiple requests with QNetworkAccessManager

  1. #1
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Question Multiple requests with QNetworkAccessManager

    Hi guys,

    there is some basic thing I do not understand about the QNetworkAccessManager.
    I was told to put only one into the Mainwindow object, used to handle every request caused by my program.
    But according to this, how do I distinguish between two request done at nearly the same time — the NAM::finished - signal sends any reply of every kind to the slot it's connected to (let's assume it's named „resp”); but there might be some different action to be applied onto one of these replies.
    For instance, at one time the program loads the XML file to find new updates. Also, there is requested The basic profile information of a facebook-user. Both is done with the NAM, so how do I find out if the incoming reply is the update or the profile data?

    Greets, Lukas

  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: Multiple requests with QNetworkAccessManager

    Have a look at what QNetworkAccessManager::get() (or put()) returns.
    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
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple requests with QNetworkAccessManager

    Ah, sounds like I should connect the finished() signal of the returned QNetworkReply to the manual handling function, like this:

    Qt Code:
    1. void Manwindow::requestUpdates()
    2. {
    3. // updateReply is declared publicly in the Mainwindow class
    4. conect(updateReply, SIGNAL(finished()), this, SLOT(handleUpdateXML()));
    5. updateReply = NAM.get( /* get Request stuff in here */ );
    6. }
    7.  
    8. void MainWindow::handleUpdateXML()
    9. {
    10. QString xml = QString::fromUTF8(updateReply->readAll());
    11. // Actions beng performed to read the XML data and so on...
    12. updateReply->deleteLater();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Well, there are possibly some syntax errors, I just wanna know if I'm right with my basic idea of doing it correctly

    thx for quick reply, Lukas

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple requests with QNetworkAccessManager

    Right general idea.
    Line 4 and 5 are the wrong way around, you need the pointer that get() or post() returns before you can connect() it to a slot.
    Line 10, you will get here on error conditions too, so best check for success.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Music For DJs Downloads mp3

    You can also use QObject::sender() to get the reply object in the finished slot
    Qt Code:
    1. void MainWindow::handleUpdateXML()
    2. {
    3. QNetworkReply *updateReply = qobject_cast<QNetworkReply*>(QObject::sender());
    4.  
    5. QString xml = QString::fromUTF8(updateReply->readAll());
    6. // Actions beng performed to read the XML data and so on...
    7. updateReply->deleteLater();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Even if you are only using one slot for all replies, you can still identify the request because each reply object contains the QNetworkRequest it was created for

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    ljuhrich (6th June 2013)

  7. #6
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up

    So the correct code would be like
    Qt Code:
    1. void Manwindow::requestUpdates()
    2. {
    3. QNetworkmanager *NAM = new QNetworkManager();
    4. // updateReply is declared publicly in the Mainwindow class (?)
    5. updateReply = NAM.get( /* get Request stuff in here */ );
    6. conect(updateReply, SIGNAL(finished()), this, SLOT(handleUpdateXML()));
    7. }
    8.  
    9. void MainWindow::handleUpdateXML()
    10. {
    11. QString xml = QString::fromUTF8(updateReply->readAll());
    12. // Actions beng performed to read the XML data and so on...
    13. updateReply->deleteLater();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Is it necessary to declare the QNetworkReply globally e.g. in the mainwindow-class or can it be defined locally too — does a updateReply->deleteLater(); prevent it to be deleted after leaving the procedure (as I learned it)?

    Greets, Lukas

    PS: thx for showing me the QObject::sender() opportunity, but I'll keep it working in separate procedures
    Last edited by ljuhrich; 6th June 2013 at 16:42. Reason: reformatted to look better

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple requests with QNetworkAccessManager

    I would not allocate a new QNetworkAccessManager for each request, just create it once (member variable or on heap) once and reuse it. Don't forget to manage the QNAM memory if you do allocate it on the heap.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple requests with QNetworkAccessManager

    Quote Originally Posted by ljuhrich View Post
    Is it necessary to declare the QNetworkReply globally e.g. in the mainwindow-class or can it be defined locally too
    It can be done locally as you can use QObject::sender() to retrieve the pointer in the slot connected to finish().

    Quote Originally Posted by ljuhrich View Post
    oes a updateReply->deleteLater(); prevent it to be deleted after leaving the procedure (as I learned it)?
    It is not being deleted automatically because the caller (your code) becomes the owner of the pointer.
    deleteLater() allows you to schedule deletion, i.e. defer it to another run of the event loop. Calling delete on the pointer inside a slot connected to the object's signal on the other hand would not be good

    Cheers,
    _

Similar Threads

  1. QNetworkAccessManager in multiple threads
    By mentalmushroom in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2013, 00:14
  2. QNetworkAccessManager and multiple QNetworkReply
    By Acid in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2012, 23:52
  3. Multiple QNetworkAccessManager requests.
    By Diath in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2011, 23:04
  4. Replies: 4
    Last Post: 19th January 2011, 14:49
  5. Tracking multiple requests with QNetwork
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 09:04

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.