QNetworkAccessManager correct stack/heap handling for QByteArray and QNetworkRequest

  • Hi,

    I am a little bit confused about the send NetworkRequest functions of the QNetworkAccessManager.
    Sadly I have only the DLL's so I can not jump into the declaration of e.g. get, post, head etc. to see what exactly happens.

    I am new in Network programming in Qt so I had a look at some sides and some tutorials and it looks like every tutorial makes it different and even the Qt function documentation is open for interpretation.

    In the official Qt tutorial I saw this.

    void HttpWindow::startRequest(QUrl url){
        reply = qnam.get(QNetworkRequest(url));

    So I saw this and thought ok the NetworkRequest is put on stack and the *.get() Function will create a copy of that and send it to the server because the stack object is destroyed after startRequest function is finished and the chance is high that in get function is a thread launched so that start request function is not blocking.

    But in another tutorial I saw this:

     void sendRequest(){
        // create custom temporary event loop on stack
        QEventLoop eventLoop;
        // "quit()" the event-loop, when the network request "finished()"
        QNetworkAccessManager mgr;
        QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
        // the HTTP request
        QNetworkRequest req( QUrl( QString("http://ip.jsontest.com/") ) );
        QNetworkReply *reply = mgr.get(req);
        eventLoop.exec(); // blocks stack until "finished()" has been called
        if (reply->error() == QNetworkReply::NoError) {
            qDebug() << "Success" <<reply->readAll();
            delete reply;

    Additional to that I saw the function description of ::sendCustomRequest() which states that if data is not empty, data must be open for reading and must remain valid until finished() signal is emitted.
    The same description is not written e.g. for QNetworkAccessManager::post() but this has the same function signature so also takes a const QByteArray & data.
    Is in the post case the data copied and in the sendCustomRequest() not or it is also necessary for the post request that QByteArray remains valid?

    My Questions:

    1. how does send functions work internally?
    2. What is the correct design to send requests (Must QNetworkRequest and QByteArray remain valid until slot function: replyFinished(QNetworkReply*) is called? That means do I have to put them on the heap or on stack in a "higher" level?

  • Lifetime Qt Champion


    1. You can find that by looking at Qt's source which is available at http://code.qt.io
    2. Like shown in the documentation, there's no need for heap allocation. You create the QNetworkRequest and the QByteArray on the stack, use them and voilĂ . Once the call returns, they both have been used. Only the QIODevice that you pass to e.g. sendCustomRequest needs to live longer.

Log in to reply

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