PDA

View Full Version : How to reuse a QImage in a While loop?



JoseTlaseca
2nd June 2008, 23:44
Hello everybody...

I'm programming a tracking system... i'm using image sequences... i have a working application that shows the image sequence, this is the basic idea:


While(condition)
{
<some code...>

image = new QImage(fileName);//fileName is changing on every loop with something like /home/image0001.bmp... image0002.bmp... etc

<some code to refresh screen and show the imagen...>
}

But there's a problem... i create a new Qimage on every loop and increases the memory consumption... that's bad...

Well... i need to load an image on every loop in the same QImage object (image), then set empty the image object to load next of the sequence and show it without increasing memory consumption...

I think the better idea is something like:


// in the .h file:
QImage *image;

//in the .cpp file, in the constructor:
image = new QImage(width, height, 32);//an empty image with some size

//then in the while loop:
While(condition)
{
<some code...>

<here i need to load an image in "image" object...>

<some code to refresh screen and show the image...>

<finally i need to empty "image" object, because i'm gonna load in it the next image of the sequence...>
}

The problem is... i don't know how to do that...

Can you help me?

I'm programming with C++/QT3

Thank you anyway...

Jose

PS: Excuse me... but my english is really bad... hope you understand...

wysota
3rd June 2008, 00:09
You don't have to create the image on heap. Create it on stack and reuse it later on.


QImage image;
int i=0;
while(i<1000){
image = QImage(QString("image%1.png").arg(i+1));
doSomething(image);
i++;
}

JoseTlaseca
3rd June 2008, 00:35
You don't have to create the image on heap. Create it on stack and reuse it later on.


QImage image;
int i=0;
while(i<1000){
image = QImage(QString("image%1.png").arg(i+1));
doSomething(image);
i++;
}
wysota, thanks for answer my question...

I understand your advice... but i cant compile it...

my QImage object is declared in the .h file like this:


QImage *image;

look that image is a pointer... so i access its methods with the -> operator, so the:


image = QImage (fileName);//fileName changing on every loop

shows the error:


"error... cannot convert 'QImage' to 'QImage*' in assignment"

I cant change the declaration of "image" for a non pointer cause there's code that uses it as is...

How do i need to do to make it works???

=( sorry, i'm really newbie.

wysota
3rd June 2008, 06:49
As I said, don't use a pointer. Declare the object as "QImage image" and not "QImage *image". If you need to pass a pointer somewhere, you can always get a pointer by using the & operator.

JoseTlaseca
4th June 2008, 15:35
As I said, don't use a pointer. Declare the object as "QImage image" and not "QImage *image". If you need to pass a pointer somewhere, you can always get a pointer by using the & operator.


wysota: thanks for your reply...

I have found a solution in the way i need to do the things...

this is how i get good results (no memory crash, refresh speed and good tracking results):

/***IN THE HEADER FILE DECLARE QIMAGE POINTER***/
QImage *image;



/***IN THE CLASS CONSTRUCTOR CREATE AN EMPTY QIMAGE OBJECT***/
image = new QImage(Width, Height, 32);



/***IN THE WHILE LOOP UPDATE IMAGE'S CONTENT***/
image->load(fileName);//fileName is changing on every loop



/**Look that there's no need to delete "image" every loop because there's only one "image" object (declared in constructor), so... just use it.**/


/***WHEN FINISH... DELETE QIMAGE POINTER, I.E. IN THE DESTRUCTOR***/
delete image;



that's all... worked for me... (if someone have the same problem...)


SO... PROBLEM... SOLVED

wysota
4th June 2008, 16:22
But why make that image object global? You can really declare it as a local object :)

JoseTlaseca
4th June 2008, 16:55
But why make that image object global? You can really declare it as a local object :)

Because i need the image live along all time not only in the thread ... ehmmm... i think the code is not perfect and.... yes... there are better ways to do the job... i promise i work on it...

Thanks...

wysota
4th June 2008, 20:53
I'm not sure QImage is thread safe.

JoseTlaseca
5th June 2008, 00:53
I'm not sure QImage is thread safe.


I had some troubles with that in past... repainting process is not thread-safe... don't know if QPixmap or QImage causes it (I use both, one to get pixel access and the other one to paint, both for the same image)... i solved it using a postEvent and works fine... Now i'm still using postEvent and there's no problem.

wysota
5th June 2008, 07:19
postEvent only schedules some action to happen but if you operate on the image from two threads, you'll get a clash anyway. You'd have to protect your image with a mutex.