PDA

View Full Version : QPixmap loadFromData() problem..



zgulser
10th May 2012, 16:13
Hi,

I have a QNetworkReply which contains a picture(PNG) as bytes.

The problem is; when I try to read all bytes in the the reply like ;



QPixmap pixmap;
pixmap.loadFromData(currentReply->readAll(), "PNG");
_tempForMSNAvatarLoading->setAvatar(pixmap);



it doesn't load the image.

However, if I first write the image to a QFile;



QFile temp("msn_temp_avatar.PNG");

if(temp.open(QIODevice::ReadWrite))
{
temp.write(currentReply->readAll());
}


and then pass the name of the file to the QPixmap like;




QPixmap pixmap(temp.fileName());


it works..

What might be the problem here?

wysota
10th May 2012, 23:20
Make sure the reply has the complete image ready before you call loadFromData().

zgulser
11th May 2012, 07:14
Hi,

I have the following code piece;



currentReply = nam->get(QNetworkRequest(QUrl(s)));

connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)), this,
SLOT(onDownloadProgress(qint64,qint64)));


and in onDownloadProgress



void SNGWManager::onDownloadProgress(qint64 p_BytesReceived,qint64 p_BytesTotal)
{
if(p_BytesReceived == p_BytesTotal)
{
if(currentReply)
{
// Pixmap loading here
}

...
}

}


I suppose that reads all the data in the reply. If doesn't, QFile wouldn't work already?

ChrisW67
11th May 2012, 07:47
Your approach should work but it may return a zero byte array under error conditions (you check for neither):

This signal is emitted to indicate the progress of the download part of this network request, if there's any. If there's no download associated with this request, this signal will be emitted once with 0 as the value of both bytesReceived and bytesTotal.
You could also check that the size of the byte array from readAll() is the same size as the p_BytesTotal.

You are better off doing this processing in response to a finished() signal after checking QNetworkReply::error() for success. Relying on the system's network buffers to hold the incoming file is probably OK for small files. If the file is particularly large then you could accumulate the response in a QByteArray or temporary file as it arrives using the readyRead() signal and complete processing in when finished() is emitted and no errors are lodged.