You have the following issues in your code:
You write the image data as if it was a text stream, whereas it is a binary stream.
You reading everything in without reading by chunks. This potentially wastes more memory than necessary.
You lack the error checking in your code.
You needlessly have the signal/slot indirection in here.
It is superfluous to check the QNetworkReply error in the finished handler slot because such things would be better caught in the error signal handler slot.
You open the file as
ReadOnly
, whereas you would like to write it.You open the file with text open mode flag, whereas an image is binary data.
I would be writing something like this:
void FriendsListProcess::picRequesFinished(QNetworkReply *reply)
{
QFile file(pic_name_path);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open the file for reading:" << file.fileName();
return;
}
// You would need to make sure you do not get irrelevant data in the meantime
while (reply->bytesAvailable()) {
if (file.write(reply->read(512)) == -1)
qDebug() << "Error while reading data:" << file.errorString();
}
}