Results 1 to 7 of 7

Thread: Can't delete widget returned by QLayoutItem->widget()

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete widget returned by QLayoutItem->widget()

    An exception is raised on the line with delete widget. glibc complains:

    free(): invalid pointer: 0xbe80291c


    Qt Code:
    1. void MyServer::clearMyLayout(void)
    2. {
    3. QLayout *currentLayout = window->layout();
    4.  
    5. if (currentLayout != NULL)
    6. {
    7. QLayoutItem* item;
    8. while ((item= currentLayout->takeAt(0)) != 0)
    9. {
    10. QWidget *widget;
    11. if ((widget = item->widget()) != 0)
    12. delete widget; // SigAbort happens here
    13. delete item;
    14. }
    15. }
    16. delete window->layout();
    17. }
    To copy to clipboard, switch view to plain text mode 

    I'm trying to remove all the widgets in a QHBoxLayout. I read that you must delete the widgets and if only delete the item, the widgets are still visible in the parent widget. I found that code fragment along with discussions about the parent being the widget, no the layout.

    But shouldn't I be able to delete any non-zero pointer returned by QLayoutItem->widget()?

    Thanks,

    Dave Thomas


    Added after 1 32 minutes:


    One of the widgets in the layout was a private data member of an object--not a pointer.

    That eliminates the exception--hoping it now works.
    Last edited by davethomaspilot; 30th May 2014 at 00:41.

  2. #2
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete widget returned by QLayoutItem->widget()

    So, deleting the widgets in a layout works. The exception was caused by trying to delete a widget which was a data member of an object.

    For confirmation, is it really necessary to delete the widgets in addition to removing them from a layout? For performance reasons, I'd rather keep the widgets around (QLabels) and just assign new QPixmaps to them.

    Dave Thomas

  3. #3
    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: Can't delete widget returned by QLayoutItem->widget()

    Quote Originally Posted by davethomaspilot View Post
    For confirmation, is it really necessary to delete the widgets in addition to removing them from a layout?
    It depends what you want to do with them afterwards. Basically you have to delete any widget without a parent. Removing a widget from a layout doesn't remove it from the parent so explicit deletion is not required as the widget will be deleted together with its parent.

    For performance reasons, I'd rather keep the widgets around (QLabels) and just assign new QPixmaps to them.
    Then do exactly that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    davethomaspilot (1st June 2014)

  5. #4
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete widget returned by QLayoutItem->widget()

    Hmm, so I want to keep the widgets (labels) but display only a subset of them. And, their pixmaps will change.

    I thought I read the only way to get them not to appear was to actually delete them.

    Then do exactly that.
    Removing a widget from a layout doesn't prevent it from displaying. It's just not in the layout.

    So, how do I prevent a widget from displaying without deleting it?

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't delete widget returned by QLayoutItem->widget()

    So, how do I prevent a widget from displaying without deleting it?
    QWidget::hide()

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

    davethomaspilot (1st June 2014)

  8. #6
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete widget returned by QLayoutItem->widget()

    If I have three widgets in s QHBoxLayout and hide 1, will the other two align as if the widget was deleted?

    If so, that's what I need!

    Dave Thomas

  9. #7
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete widget returned by QLayoutItem->widget()

    Yes, that works fine.

    So simple--I wasted SO much time chasing a catastrophic memory leak.

    I couldn't isolate the problem, but it was somewhere where I was deleting widgets from the HBoxLayout, creating them again, assigning pixmaps, etc.

    Valgrind didn't complain, but available memory would continue to decrease until the app was killed (after a couple of hours).

    Anyway, I got rid of all that dynamic memory allocation. The widgets are always in the layout (and a containing widget). But they get new pixmaps and subsets are dynamically setVisible(true/false);

    Just what I wanted.

    Thanks!

    Dave Thomas

Similar Threads

  1. Delete single widget from QLayoutGrid
    By KeineAhnung in forum Newbie
    Replies: 8
    Last Post: 13th May 2014, 13:45
  2. Replies: 2
    Last Post: 19th July 2013, 16:24
  3. Replies: 2
    Last Post: 15th March 2012, 07:48
  4. Delete content widget in layout
    By Hogwarts in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2010, 16:59
  5. Replies: 2
    Last Post: 2nd November 2010, 01:43

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.