Results 1 to 8 of 8

Thread: How to display a QByteArray as an Image ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to display a QByteArray as an Image ?

    I have a QByteArray and this one holds an image.

    How can I display the image from my QByteArray in a QLabel ?

  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: How to display a QByteArray as an Image ?

    Qt Code:
    1. QPixmap image;
    2. image.loadFromData(ba);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display a QByteArray as an Image ?

    This is my code, and note that the "image.png" is an image located in the same folder as the source code:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication *application = new QApplication(argc, argv);
    4. QWidget *widget = new QWidget();
    5. widget->setFixedSize(500, 500);
    6. widget->show();
    7. QLabel *label = new QLabel(widget);
    8. label->setGeometry(10, 10, 480, 480);
    9. label->show();
    10. QByteArray *temp = new QByteArray();
    11. QFile *file = new QFile("image.png");
    12. *temp = file->readAll();
    13. QPixmap *pix = new QPixmap();
    14. pix->loadFromData(*temp);
    15. label->setPixmap(*pix);
    16. return application->exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    This gives me a message saying:
    QPixmap::convertFromImage: Cannot convert a null image

    What am I missing ?

  4. #4
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display a QByteArray as an Image ?

    Thanks for the help. I found the error which was that I was not opening the file

  5. #5
    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: How to display a QByteArray as an Image ?

    Why don't you just load the picture directly from a file?

    Qt Code:
    1. QPixmap pix("image.png");
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display a QByteArray as an Image ?

    I don't load the picture from the file, because the picture is not located in the local hard disk, so I need to pass this picture from one computer to another.

    Is there any other solution ?

  7. #7
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display a QByteArray as an Image ?

    how about displaying the image contained in the QByteArray in a QTextBrowser.

    Is it possible, how ?

  8. #8
    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: How to display a QByteArray as an Image ?

    Quote Originally Posted by probine
    how about displaying the image contained in the QByteArray in a QTextBrowser.

    Is it possible, how ?
    Make a pixmap out of it like shown in previous posts and load it into the text browser from a document that will reference it (using img src="xxx"). Just make sure this pixmap is available to the browser -- read the docs to see how it's done.

Similar Threads

  1. Replies: 2
    Last Post: 29th September 2008, 00:08
  2. Replies: 7
    Last Post: 13th August 2008, 18:27
  3. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

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.