Results 1 to 6 of 6

Thread: Creating QImage from char*

  1. #1
    Join Date
    May 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Creating QImage from char*

    Hi, i'm using a proper by myself written C++ code. I have a Database class that gets png/jpg files from a sqlite database. When i get the pictures i have a char*. Actually to test if it works correctly, i write the images to files, and it works.
    Now i wanted to pass this char* to QT and creating a QImage to load in a GUI. I've tried to Use QPixmal and QByteArray, but i think the problem is during the passing of the char* to the QT classes.
    Can anyone help me to arrange this?
    Please...
    Last edited by emaborsa; 8th May 2011 at 19:42.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Creating QImage from char*

    As your character data contains the contents of a file, the only way [in practice] is to just write the data into a file and the read back in in using QImage constructor (QImage(filename, ...).

  3. #3
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating QImage from char*

    Does QPixmap::loadFromData() works ?

  4. #4
    Join Date
    May 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating QImage from char*

    Quote Originally Posted by mvuori View Post
    As your character data contains the contents of a file, the only way [in practice] is to just write the data into a file and the read back in in using QImage constructor (QImage(filename, ...).
    Really? Damn... in Java i've seen the ByteArray class, which takes a bytearray and an ImageIcon can be created. Since i've seen the same classes (QBytearray + QImage) in QT i thought taht it would be possible.

    Quote Originally Posted by rsilva View Post
    Does QPixmap::loadFromData() works ?
    Yes it works, but only for file which are already on disc. The problem is that i have to eliminate those files when i no longer use it, and if the system crashes.....there would be a memory leak.

    Maybe i have to specify that the char* is the result of a sqlite query with sqlite3_column_blob() statement:
    Last edited by emaborsa; 8th May 2011 at 20:04.

  5. #5
    Join Date
    Apr 2011
    Posts
    61
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating QImage from char*

    No, that mvuori said is not true. The class must load the image from the file to get the bytes and then load. You CAN load a image from unsigned char array.
    It's really possible.

    How are you using loadFromData ?

    Can you try using it without using a QByteArray ?

    Qt Code:
    1. bool QPixmap::loadFromData ( const uchar * data, uint len, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )
    To copy to clipboard, switch view to plain text mode 

    I think it can be some problems while converting characters, I dunno.
    But, try to use loadFromData with your downloaded char* directly.

  6. #6
    Join Date
    May 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating QImage from char*

    Quote Originally Posted by rsilva View Post
    No, that mvuori said is not true. The class must load the image from the file to get the bytes and then load. You CAN load a image from unsigned char array.
    It's really possible.

    How are you using loadFromData ?

    Can you try using it without using a QByteArray ?

    Qt Code:
    1. bool QPixmap::loadFromData ( const uchar * data, uint len, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )
    To copy to clipboard, switch view to plain text mode 

    I think it can be some problems while converting characters, I dunno.
    But, try to use loadFromData with your downloaded char* directly.
    thanks rsilva. it works. I think i'll build a statue for you thanks a lot!!!!!

Similar Threads

  1. Creating a pixmap from unsigned char*
    By winder in forum Qt Programming
    Replies: 9
    Last Post: 6th July 2021, 13:56
  2. const char* QImage Format?
    By grabalon in forum Newbie
    Replies: 2
    Last Post: 13th May 2010, 00:44
  3. Creating a QImage from uchar* data
    By forrestfsu in forum Qt Programming
    Replies: 6
    Last Post: 8th February 2007, 15:21
  4. QImage and 8 bit unsigner char **
    By fyanardi in forum Newbie
    Replies: 4
    Last Post: 6th June 2006, 14:13
  5. QImage from 8 bit char* RGB buffer
    By tboloo in forum Qt Programming
    Replies: 13
    Last Post: 15th April 2006, 19:56

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.