PDA

View Full Version : Question about QImage



paolom
9th September 2010, 10:44
Hi, I've a question about QImage.

I create a QImage, passing a uchar * buffer.

This QImage, is a property of a class.

When I destroy the object of this class, I need to destroy the buffer of the QImage.

How can I get this buffer from the QImage ?

bits() return a deep copy.

I think that it's possible to get the original buffer, without save it as property of the class.

Many thanks,

Paolo

tbscope
9th September 2010, 10:50
The const bits() function does not return a deep copy

paolom
9th September 2010, 10:52
How can I call the bits() const instead the bits() function ?

tbscope
9th September 2010, 11:03
Using const uchar * of course.

But:
I would probably do this differently. I would keep track of the data and delete it at the appropriate place.
You could keep a reference list of data and images. When the reference drops below 0, delete the data.

paolom
9th September 2010, 11:14
Sorry, but I want to understand a thing.

How can I call the overaloaded function bits() const instead the bits()?!?!

It depends about the constructor of the qimage??

Ex.

qimage.bits() which values returns ?

tbscope
9th September 2010, 11:17
uchar *data = myImage.bits(); // Deep copy
const uchar *data = myImage.bits(); // No deep copy

Dan Milburn
9th September 2010, 13:19
Just to be clear, the non-const bits() method does not create a deep copy of the memory unless there are other copies of the image sharing the same data. All it does is call detach(). If you are sure that that's the only copy of the image then calling it will work. If not then you cannot safely delete the memory in any case. I would agree that there are better ways to do this. In most situations the best thing to do is to allow QImage to manage its own memory.

paolom
9th September 2010, 13:54
Just to be clear, the non-const bits() method does not create a deep copy of the memory unless there are other copies of the image sharing the same data. All it does is call detach(). If you are sure that that's the only copy of the image then calling it will work. If not then you cannot safely delete the memory in any case. I would agree that there are better ways to do this. In most situations the best thing to do is to allow QImage to manage its own memory.

How is it possible to allow QImage to manage its own memory?

Dan Milburn
9th September 2010, 17:26
How is it possible to allow QImage to manage its own memory?

Don't use the constructor that takes an existing buffer. If you use e.g. QImage ( int width, int height, Format format ) it will manage its own memory. Obviously this is sub-optimal if you happen to already have the image data in memory, but it will make resource management much easier.

paolom
9th September 2010, 18:47
Don't use the constructor that takes an existing buffer. If you use e.g. QImage ( int width, int height, Format format ) it will manage its own memory. Obviously this is sub-optimal if you happen to already have the image data in memory, but it will make resource management much easie

I've an existing buffer in memory.
I can use the QImage ( int width, int height, Format format ) constructor also, but how I can fill the qimage with my buffer and leave the QImage manage its own memory ?!

Ex. setPixel() ???????