Solved Pass pointer over QNetworkReply slot
-
I have a class called
Resolver
method that is calledhandleResult(Result *result)
.
The classResult
has a member that is aQUrl
and others.
ThishandleResult
method creates a request using the url from theresult
pointer and connects theQNetworkReply
signal tohandlePage
.
The thing here is that I need thatresult
pointer in thehandlePage
slot as well so I can modify the members in there.
Beforehand, I can't put theResult
as a member ofResolver
cause thehandleResult
is being called several times, meaning that it won't probably be the rightResult
whenhandlePage
is called (asQNetworkAccessManager
is async).
So how can I pass that pointer tohandlePage
? -
@Mr-Gisa
can you show some code? Especially where you create the network reply and connect it to handlePage().
I guess the easiest would probably be to connect the network reply to a lambda slot and capture all the pointers/data you need. -
std::function
. for example:void handlePage(QNetworkReply* sender, Result *result);
connect(reply,&QNetworkReply::finished,this,std::bind(&MyClass::handlePage,this,reply,result));
you need to make sure result doesn't get deleted in the meantime though
-
to add up to @VRonin the lambda approach:
QNetworkReply* reply = ... Result* result = ... connect( reply, &QNetworkReply::finished, this, [this, reply, result]() { // do whatever you have to do directly in here or call "this->handleResult(reply, result);" delete reply; delete result; });