PDA

View Full Version : Creating QImage from char*



emaborsa
8th May 2011, 20:14
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...

mvuori
8th May 2011, 20:26
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, ...).

rsilva
8th May 2011, 20:27
Does QPixmap::loadFromData() works ?

emaborsa
8th May 2011, 20:51
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.


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:

rsilva
8th May 2011, 21:10
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 ?


bool QPixmap::loadFromData ( const uchar * data, uint len, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

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

emaborsa
8th May 2011, 21:47
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 ?


bool QPixmap::loadFromData ( const uchar * data, uint len, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )

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 :D thanks a lot!!!!!