Results 1 to 6 of 6

Thread: Deleting children of a widget

  1. #1
    Join Date
    Apr 2009
    Location
    www.JaminGrey.com
    Posts
    71
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Deleting children of a widget

    I have a QScrollArea and a QWidget in it, and I don't want to delete the widget, but I want to delete all the children and the layout in the widget. What is the official Qt way of doing that?
    Last edited by ComServant; 24th September 2011 at 04:07.

  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: Deleting children of a widget

    There is no official way. Just delete the objects you don't want.
    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.


  3. #3
    Join Date
    Oct 2011
    Location
    Australia
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Deleting children of a widget

    What? I am very new to Qt and am having trouble trying to understand the layout model (thats why I came here). But anyway from my initial research cant you do the following (C++) letting x be a widget with so called "children", y being a pointer to such a child widget:

    x->layout()->removeWidget( y );

    ?


    Added after 1 11 minutes:


    Oh excuse me. I had assumed "layouts" were the only mechanism for adding "children" to a QWidget. But since QWidget extends QObject and QObject is some sort of node like object theres two mechanisms by which a QWidget can have children, or at least a type of descendants (Why did Qt feel the need make Qlayout a damn container then? Seems Bazaar...). And there does not appear to be any mechanisms for removing children directly. On reading the QObject docs it seems like the method suggested by wysota *is* the official method (http://doc.qt.nokia.com/4.7/qobject.html):

    "QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren()."

    So you want to do: delete x->layout()
    Last edited by 33333; 6th October 2011 at 05:26.

  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: Deleting children of a widget

    As far as I remember, deleting a layout doesn't remove child widgets from the widget since the layout is not the parent of child widgets inserted into the widget the layout manages.
    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.


  5. #5
    Join Date
    Oct 2011
    Location
    Australia
    Posts
    29
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Deleting children of a widget

    Ah yes now I see Im an idiot, you are correct wysota. I thought I have figured out the (insane IMHHHO) object model of Qt but I had not. Now I have.

    Note (http://doc.qt.nokia.com/4.7/layout.html):

    When you use a layout, you do not need to pass a parent when constructing the child widgets. The layout will automatically reparent the widgets (using QWidget::setParent()) so that they are children of the widget on which the layout is installed.

    Note: Widgets in a layout are children of the widget on which the layout is installed, not of the layout itself. Widgets can only have other widgets as parent, not layouts.

    You can nest layouts using addLayout() on a layout; the inner layout then becomes a child of the layout it is inserted into.
    So even though Layouts can semantically and syntactically contain Widgets (void QLayout::addWidget ( QWidget * w )) those widgets are always (re)parented to the Widget that owns the given layout - if any. That is the relationship between a Layout and a widget is never parent child in the formal Qt sense.

    And, Layouts can also contain layouts which can contain Layouts ... which can contain widgets. Such widgets are also (re)parented to the Widget that owns the ~top level Layout. However the contained layouts are actually children and descendants (in the formal Qt sense) of the Layout.

    Yep insane.


    Added after 8 minutes:


    Yep insane.
    Then again got nothing on Java Swing!
    Last edited by 33333; 6th October 2011 at 10:43.

  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: Deleting children of a widget

    There is nothing insane about Qt object model. I think you are overcomplicating things. Qt object model is a simple parent-child relationship of widgets, nothing more, nothing less. Layouts of widgets is a totally independent mechanism, with the only exception that adding a widget to a layout reparents it to the widget the layout manages which is compliant to a classic rule that a child widget occupies a subarea of its parent. You have one hierarchy of widgets (or actually objects) and a second hierarchy of layout items.
    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.


Similar Threads

  1. Deleting rows (children) from a qstandarditem
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 29th March 2012, 16:04
  2. focus events of widget children
    By daemonna in forum Newbie
    Replies: 1
    Last Post: 25th January 2011, 20:15
  3. deleting a widget
    By john_god in forum General Programming
    Replies: 1
    Last Post: 30th December 2010, 09:36
  4. Replies: 2
    Last Post: 7th July 2010, 14:49
  5. Replies: 0
    Last Post: 15th March 2010, 06:08

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.