View Full Version : Properly process QNetworkAccessManager response

18th November 2014, 22:16
How properly pass callback or call user function while finish() emmited?
I can't pass like that:

typedef void(*ptr)(const void *);
public slots:
void reply(QNetworkReply* reply, ptr);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*, ptr)));

Due to incompatible sender/receiver arguments.

Please explain some response processing "technics".

Best regards.

19th November 2014, 17:52
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?


19th November 2014, 20:35
Don't use the manager finished() signal at all.

QNetworkReply *reply = manager->get(request);
connect(reply, SIGNAL(finished()), someObject, SLOT(doStuff()));

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.

21st November 2014, 08:14
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());