Question

I'm downloading a file using QNetworkAccessManager::get but unlike QHttp::get there's no built-in way to directly write the response to a different QIODevice.

The easiest way would be to do something like this:

QIODevice* device;

QNetworkReply* reply = manager.get(url);
connect(reply, SIGNAL(readyRead()), this, SLOT(newData()));

and then in newData slot:

device->write(reply->readAll());

But I'm not sure if this is the right way, maybe I missed something.

Was it helpful?

Solution

That looks correct. I would use the lower-level forms of read() and write(), not the QByteArray ones, which do not properly support error handling, but other than that, it looks fine.

Are you having problems with it?

OTHER TIPS

Better use the finished signal to read all the contents at the end of the download process. An example (remove the event loop and use a new slot to make it asynchronous):

    QNetworkAccessManager manager;
    QEventLoop loop;
    QNetworkReply *reply = manager.get( request );
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));

    loop.exec();

    QFile file( "YOUR FILE" );
    file.open(QIODevice::WriteOnly);
    file.write(reply->readAll());

    delete reply;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top