Results 1 to 4 of 4

Thread: QPixmap loadFromData() problem..

  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QPixmap loadFromData() problem..

    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 ;

    Qt Code:
    1. QPixmap pixmap;
    2. pixmap.loadFromData(currentReply->readAll(), "PNG");
    3. _tempForMSNAvatarLoading->setAvatar(pixmap);
    To copy to clipboard, switch view to plain text mode 

    it doesn't load the image.

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

    Qt Code:
    1. QFile temp("msn_temp_avatar.PNG");
    2.  
    3. if(temp.open(QIODevice::ReadWrite))
    4. {
    5. temp.write(currentReply->readAll());
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QPixmap pixmap(temp.fileName());
    To copy to clipboard, switch view to plain text mode 

    it works..

    What might be the problem here?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPixmap loadFromData() problem..

    Make sure the reply has the complete image ready before you call loadFromData().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QPixmap loadFromData() problem..

    Hi,

    I have the following code piece;

    Qt Code:
    1. currentReply = nam->get(QNetworkRequest(QUrl(s)));
    2.  
    3. connect(currentReply, SIGNAL(downloadProgress(qint64,qint64)), this,
    4. SLOT(onDownloadProgress(qint64,qint64)));
    To copy to clipboard, switch view to plain text mode 

    and in onDownloadProgress

    Qt Code:
    1. void SNGWManager::onDownloadProgress(qint64 p_BytesReceived,qint64 p_BytesTotal)
    2. {
    3. if(p_BytesReceived == p_BytesTotal)
    4. {
    5. if(currentReply)
    6. {
    7. // Pixmap loading here
    8. }
    9.  
    10. ...
    11. }
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPixmap loadFromData() problem..

    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.

Similar Threads

  1. QImage.loadFromData won't work
    By cil in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2011, 16:22
  2. loadFromData with Pixmap probelm
    By December in forum Qt Programming
    Replies: 5
    Last Post: 12th June 2008, 23:16
  3. QImage loadFromData
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 18:57
  4. QImage::loadFromData() behaviour
    By Caius Aérobus in forum Qt Programming
    Replies: 10
    Last Post: 29th November 2007, 21:21
  5. how to use loadFromData in painter
    By sar_van81 in forum Qt Programming
    Replies: 18
    Last Post: 31st January 2007, 04:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.