[Qt-interest] Is there any way to dynamically rearrange a dialog'slayout?

Ross Bencina rossb-lists at audiomulch.com
Thu May 20 05:46:16 CEST 2010


>Would explicitly setting the parent to NULL also remove the reference to the child from a parent object 

Yes.

In all cases (including deletion) Qt will (should) maintain the integrity of parent-child relationships. Deleting a child removes it from the parent. Changing the parent using setParent() updates the new and old parents (if any). There are other cases (non-parent/child cases) where you may need to be a little careful of object lifetimes, but for parent child stuff it's all consistent. What isn't consistent (or at least not clear to me) is when parents are automatically set or reassigned in layouts and when they aren't, and I think that is what's tripping you up.

I suspect in your case the problem is with deleted items remaining in your pointer lists, possibly leading to double deletions, or with items getting deleted by their parents.

Ross.



  ----- Original Message ----- 
  From: Ellen Kestrel 
  To: Ross Bencina 
  Cc: Qt-Interest 
  Sent: Thursday, May 20, 2010 1:11 PM
  Subject: Re: [Qt-interest] Is there any way to dynamically rearrange a dialog'slayout?


  Thanks for the suggestions.  Would explicitly setting the parent to NULL also remove the reference to the child from a parent object (if, for example, the dialog or the parent layout still think that they layout or widget is their child...) as well, or might I have to do that manually also?  Or would that not matter?  In all cases I've tried to be careful to delete the former children of a layout before deleting the layout itself, after removing them from the layout, of course.  If I do it the other way, will the parent layout take care of deleting all of its children, or will it leave them with an invalid pointer to the now-deleted layout?

  EK


  On Wed, May 19, 2010 at 6:46 PM, Ross Bencina <rossb-lists at audiomulch.com> wrote:

    Hi Ellen

    Just a guess, but it sounds like some of your widgets are getting deleted when you delete the layouts -- ie, somewhere you have something which is the child of something you're deleting which shouldn't be.

    The documentation is a bit confusing regarding transfer of ownership in different cases (see Layout::addChildWidget, QLayout::addItem, QLayout::removeWidget etc). In any case, it's worth keeping in mind that the ownership graph(parent/child relationship) is not always the same as the layout nesting graph. One thing you could try is to make sure, after removing something from a layout, you reset its parent to your Dialog (or to NULL), that way it's lifetime won't be tied to anything you care about.

    QObject::dumpObjectTree() might be helpful for debugging this.

    You could also consider keeping your pointer lists up to date by connecting to QObject::destroyed() on each object. Keep in mind that objects may be deleted using deleteLater() so this signal isn't always synchronous.

    HTH

    Ross.

    ----- Original Message ----- From: Ellen Kestrel
    To: qt-interest at trolltech.com
    Sent: Thursday, May 20, 2010 10:49 AM
    Subject: [Qt-interest] Is there any way to dynamically rearrange a dialog'slayout?



    I have a dialog whose main layout is a QVBoxLayout that contains mostly a series of identically structured QHBoxLayouts.  The QHBoxLayouts, and the widgets in them, are all stored in lists of QPointers as members of the dialog.  I want to make it so that more of these layouts can be added to the dialog dynamically, so I made slots that added or removed widgets and layouts to and from these lists of pointers (without actually deleting anything) and then called an updating function that first removes everything from the main layout and deletes any layout items that are not in one of the lists of pointers, and then adds everything that is in a list back into the main layout.  All of these functions check and recheck for the lists having the wrong number of items, having null pointers, etc., and print error messages if anything is wrong.  Yet, in addition to the main layout getting screwed up and laying out sublayouts on top of other sublayouts, it crashes at seemingly random times without printing any of my error messages.  What it does print to the terminal begins thusly:

    *** glibc detected *** ./ExecutableName: free(): invalid next size (fast): 0x09778378 ***
    ======= Backtrace: =========
    ...

    It also constantly prints this message, whether it crashes or not:

    QLayout::addChildLayout: layout "" already has a parent

    It looks like QLayout::removeItem is not properly removing layouts from other layouts.  I did not create any of these layouts with parent widgets, since setting the layouts takes care of that.

    Again, at every single point where there could be a null pointer to cause a crash, *there is error checking*, none of my error messages are ever printed, and neither is there a standard segfault message.  Is there any way to do this, or should I just give up?

    EK





    _______________________________________________
    Qt-interest mailing list
    Qt-interest at trolltech.com
    http://lists.trolltech.com/mailman/listinfo/qt-interest 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100520/4a3f135f/attachment.html 


More information about the Qt-interest-old mailing list