Results 1 to 10 of 10

Thread: How to reuse a QImage in a While loop?

  1. #1
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default How to reuse a QImage in a While loop?

    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:

    Qt Code:
    1. While(condition)
    2. {
    3. <some code...>
    4.  
    5. image = new QImage(fileName);//fileName is changing on every loop with something like /home/image0001.bmp... image0002.bmp... etc
    6.  
    7. <some code to refresh screen and show the imagen...>
    8. }
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. // in the .h file:
    2. QImage *image;
    3.  
    4. //in the .cpp file, in the constructor:
    5. image = new QImage(width, height, 32);//an empty image with some size
    6.  
    7. //then in the while loop:
    8. While(condition)
    9. {
    10. <some code...>
    11.  
    12. <here i need to load an image in "image" object...>
    13.  
    14. <some code to refresh screen and show the image...>
    15.  
    16. <finally i need to empty "image" object, because i'm gonna load in it the next image of the sequence...>
    17. }
    To copy to clipboard, switch view to plain text mode 
    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...
    Last edited by jpn; 3rd June 2008 at 07:20. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to reuse a QImage in a While loop?

    You don't have to create the image on heap. Create it on stack and reuse it later on.

    Qt Code:
    1. QImage image;
    2. int i=0;
    3. while(i<1000){
    4. image = QImage(QString("image%1.png").arg(i+1));
    5. doSomething(image);
    6. i++;
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to reuse a QImage in a While loop?

    Quote Originally Posted by wysota View Post
    You don't have to create the image on heap. Create it on stack and reuse it later on.

    Qt Code:
    1. QImage image;
    2. int i=0;
    3. while(i<1000){
    4. image = QImage(QString("image%1.png").arg(i+1));
    5. doSomething(image);
    6. i++;
    7. }
    To copy to clipboard, switch view to plain text mode 
    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:

    Qt Code:
    1. QImage *image;
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. image = QImage (fileName);//fileName changing on every loop
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jpn; 3rd June 2008 at 07:21. Reason: tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to reuse a QImage in a While loop?

    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.

  5. #5
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to reuse a QImage in a While loop? (SOLVED)

    Quote Originally Posted by wysota View Post
    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
    Last edited by JoseTlaseca; 4th June 2008 at 15:43. Reason: SOLVED

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to reuse a QImage in a While loop? (SOLVED)

    But why make that image object global? You can really declare it as a local object

  7. The following user says thank you to wysota for this useful post:

    JoseTlaseca (4th June 2008)

  8. #7
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to reuse a QImage in a While loop? (SOLVED)

    Quote Originally Posted by wysota View Post
    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...

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to reuse a QImage in a While loop? (SOLVED)

    I'm not sure QImage is thread safe.

  10. #9
    Join Date
    Jun 2008
    Posts
    11
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to reuse a QImage in a While loop? (SOLVED)

    Quote Originally Posted by wysota View Post
    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.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to reuse a QImage in a While loop? (SOLVED)

    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.

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 12:40
  3. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  4. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.