QNetworkAccessManager better way of using it

  • I'm new to Qt and it's design as I come from other languages and I have a question regarding QNetworkAccessManager and the way it works with signal and slots. I noticed that if I need to do a request using QNetworkAccessManager I need to connect to a slot in order to get the return using QNetworkReply but as I come from other languages that a simple request I could just set to a variable instead of needing to connect the return to another method, I was wondering:

    If I need to create an API wrapper like for example: I need to create a request to authenticate, another request to query, another request to download, how would that look like? Is there an example of making more than one request in the same class to an API?

  • Qt Champions 2016

    The reason for the slots is that QNetworkAccessManager is asyncon.
    So it will not wait/stay at the line until something received but "call you back" instead

    However since you can now use lambdas you can keep it very local (demo code, not complete)

    QNetworkRequest request = QNetworkRequest(QUrl(xxxxx));
    QNetworkReply* reply = m_manager->get(request);
    connect(reply, &QNetworkReply::finished, [this, reply]() // <-- lamda as slot
        qDebug() << sender();
        qDebug() << reply->errorString();
        qDebug() << reply->readAll(); 

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.