Results 1 to 8 of 8

Thread: Displaying pixmaps and memory leak ??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Displaying pixmaps and memory leak ??

    Ok so clearly this is not the way to do things...

    I'm struggling to work out how to draw a QImage or QPixmap onto the GUI though. I can find loads of examples but they are not QT creator applications using the QGraphicsView object. I can set the scene etc but how to set it to use my image without using something like addPixmap which doesn't remove the old pixmap?

  2. #2
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Displaying pixmaps and memory leak ??

    Basically, calling QGraphicsScene::addPixmap() will return a QGraphicsPixmapItem. So all you have to do is to hold the QGraphicsPixmapItem pointer when adding a pixmap. When adding another pixmap, delete the previous QGraphicsPixmapItem first.

    Qt Code:
    1. void MainWindow::display_image()
    2. {
    3. ...
    4. lpixmap = QPixmap::fromImage(limage);
    5. if(myPixmapItem)
    6. delete myPixmapItem;
    7. myPixmapItem= left_cam_view->addPixmap(lpixmap);
    8. }
    To copy to clipboard, switch view to plain text mode 

    the myPixmapItem is a member of MainWindow, a pointer to QGraphicsPixmapItem. Remember to set it to 0 when constructing a MainWindow Object.



    Another way is,
    Qt Code:
    1. void MainWindow::init()
    2. {
    3. ...
    4. myPixmapItem = new QGraphicsPixmapItem();
    5. left_cam_view->addItem(myPixmapItem);
    6. ...
    7. }
    8.  
    9. void MainWindow::display_image()
    10. {
    11. ...
    12. QPixmap lpixmap = QPixmap::fromImage(image);
    13. myPixmapItem->setPixmap(lpixmap);
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 
    In this way, you will only have a QGraphicsPixmapItem and no more creating and deleting. This one would be better solution. I'm sure calling QGraphicsPixmapItem::setPixmap() will clear the previous QPixmap.

    Some other things are that you're creating a QImage and a QPixmap in the stack every time the display_image() is called. This will cause constructing and destructing them. I think it's better to make them members of the MainWindow. And then you don't have to construct and destruct them. It shouldn't take too much memory, since QPixmap is implicit sharing.
    Last edited by MorrisLiang; 29th June 2010 at 09:59.
    It's not the goodbye that hurts,but the flashback that follow.

  3. The following user says thank you to MorrisLiang for this useful post:

    SiddhantR (2nd June 2014)

  4. #3
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Displaying pixmaps and memory leak ??

    Ok so before I saw the last post I discovered that clearing the scene fixed the problem, but it looks like the last post gives a more elegant solution. Thanks for you help with this!

Similar Threads

  1. Memory leak
    By yxtx1984 in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2010, 11:13
  2. Memory leak of Qt?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2009, 23:32
  3. memory leak
    By mattia in forum Newbie
    Replies: 18
    Last Post: 16th January 2008, 10:22
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  5. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31

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
  •  
Qt is a trademark of The Qt Company.