Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pass pointer over QNetworkReply slot



  • I have a class called Resolver method that is called handleResult(Result *result).
    The class Result has a member that is a QUrl and others.
    This handleResult method creates a request using the url from the result pointer and connects the QNetworkReply signal to handlePage.
    The thing here is that I need that result pointer in the handlePage slot as well so I can modify the members in there.
    Beforehand, I can't put the Result as a member of Resolver cause the handleResult is being called several times, meaning that it won't probably be the right Result when handlePage is called (as QNetworkAccessManager is async).
    So how can I pass that pointer to handlePage?


  • Moderators

    @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


  • Moderators

    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;
    });
    

Log in to reply