View Full Version : QImage Issue

2nd February 2007, 07:48
Hi All,
I m using QT 4.2.2 on my Intel Mac.
I have a unsigned char * buffer.....
unsigned char *pBuff= new unsigned char[SizeToRead];

I gave some bytes read from the harddisk to construct a image and I m using the following to construct a image from the above Buffer

QImage::QImage ( uchar * data, int width, int height, Format format )

But Actually I donot know the format of the image so I can not able to give the parameter Format value.

So How can I know the format of the image from the Bytes I have read or there is another method to construct an image from uchar * buffer.


2nd February 2007, 07:53
Will this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-creating-a-qimage-from-uchar-data-5413.html) be useful to you ??

2nd February 2007, 11:43
I m usng the following to create image from the uchar *......

bool QImage::loadFromData ( const uchar * data, int len, const char * format = 0 )

When i check this return vaue it is coming out to be true and when I m checking that....

if(image.isNull()) then it is returning false that is it is creating image but when i m setting that image in the pixmap using..

image=image.scaled(100,100); QPixmap pixmap;

and showing that in the QListWidget it is not showing the image I m using the following code

QListWidgetItem *LWidgetItem=new QListWidgetItem(pixmap,"HI",listWidget,0);

But it is showing the text HI not the image.

If any body knows why then plz help me again.


2nd February 2007, 13:00
QPixmap::fromImage() is a static method returning the pixmap:

QImage img;
QPixmap pix = QPixmap::fromImage(img);

2nd February 2007, 13:06

Are you sure that you are not destructing the memory data buffer?
In the QImage contructor you can read this:

"The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction."

So, I think that you have to mantain the data buffer into memory. Try to execute "bool bOK = image.isNull();" just after "loadFromData" call.

5th February 2007, 05:29
Thanks to all of you.
Now I m able to do that.