Results 1 to 4 of 4

Thread: Properly process QNetworkAccessManager response

  1. #1
    Join Date
    Nov 2014
    Posts
    8
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Properly process QNetworkAccessManager response

    Hello!
    How properly pass callback or call user function while finish() emmited?
    I can't pass like that:
    Qt Code:
    1. typedef void(*ptr)(const void *);
    2. ...
    3. public slots:
    4. void reply(QNetworkReply* reply, ptr);
    5. ...
    6. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*, ptr)));
    To copy to clipboard, switch view to plain text mode 
    Due to incompatible sender/receiver arguments.

    Please explain some response processing "technics".

    Best regards.

  2. #2
    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: Properly process QNetworkAccessManager response

    The slot cannot have more arguments than the signal.
    How would the program know how to create the data for the additional ones?

    Why do you even need another callback. The slot is the callback for the signal, no?

    Cheers,
    _

  3. #3
    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: Properly process QNetworkAccessManager response

    Don't use the manager finished() signal at all.
    Qt Code:
    1. QNetworkReply *reply = manager->get(request);
    2. connect(reply, SIGNAL(finished()), someObject, SLOT(doStuff()));
    To copy to clipboard, switch view to plain text mode 
    Where doStuff() is what your function pointer used to point at, re-declared as a slot. If the function pointer target is outside of Qt then doStuff() is a simple slot wrapper in your code that calls the external function. Every request can be directed to a different slot, no need to pass a function pointer.

    If you cannot control where the pointer is going, i.e. It is passed in from outside and is not from a limited set known at compile time, then use a map to store the reply pointer and its corresponding call back pointer . When the finished signal is received from a reply look up up the callback pointer based on the sender (or the passed QNetworkReply if you use the manager's finished signal). Make sure you remove the used entry from the map or it will grow indefinitely.
    Last edited by ChrisW67; 19th November 2014 at 20:47.

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Properly process QNetworkAccessManager response

    To add to what ChrisW67 said, you can get the reply pointer inside of the doStuff() slot as follows:

    QNetworkReply* reply = dynamic_cast<QNetworkReply*>(sender());

Similar Threads

  1. Replies: 1
    Last Post: 16th April 2014, 07:44
  2. detecting process by pid/process name
    By cic in forum Newbie
    Replies: 0
    Last Post: 18th June 2013, 13:44
  3. Reqest and Response in XML
    By srohit24 in forum Qt Programming
    Replies: 28
    Last Post: 4th July 2009, 07:57
  4. How to communicate Qt Process with non-qt process
    By nrabara in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 15th February 2009, 21:01
  5. Replies: 1
    Last Post: 13th September 2008, 14:45

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.