Results 1 to 3 of 3

Thread: What owns what?

  1. #1
    Join Date
    May 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question What owns what?

    I know QT has a parent/child hierarchy way of storing QObject's.

    1. But what happens to root QWidgets? They do not have a constructor that takes a QObject parent. It is possible to make a QObject that does not inherit QWidget the parent of a QWidget?
    2. What happens if you add a QWidget to a QScene? Does the QScene now owns the QWidget or does the QGraphicsViewProxy now owns the QWidget?
    3. What owns the QGraphicsViewProxy objects that is returned by scene->addWidget(new_widget)?
    4. Will a widget take ownership of a layout? E.g. what happens with root_widget->setLayout(new_layout)?
    5. Will the layout take ownership if I add a widget/layout to a root layout, e.g. root_layout->addWidget(new_widget) or root_layout->addLayout(new_sub_layout)?
    6. If I have an application that dynamically creates objects and setup QT signal connections between the events with QObject::connect(), is it safe to assume that the connections will automatically be deleted when either one of the objects they used to connect gets deleted?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: What owns what?

    1) "Root" QWidget is the widget that don't have a parent at construction if you don't set parameter for parent it is default 0, witch means the widget is a window/used for "parenting" other widgets.

    2) Not sure what you meant. Why you want a simple QWidget in anything else? A QWidget has only basic stuff for containing other widgets (derived from QWidget and implementing some functionality)

    3) I don't know the QGraphicsViewProxy, but basic rule is: if you add a widget to some container it will be auto-parented to that.

    4/5) No, the layout is not really implicated in parent/child relation:
    Qt Code:
    1. QVBoxLayout *lay = new QVBoxLayout(window);
    To copy to clipboard, switch view to plain text mode 
    This means that if you add some widgets to the layout that widgets will become child-widgets of the "window" widget.

    6) I don't know, but why do you want some of that? It is your job to make sure that widgets that "need" each-other will be "alive" in the same time, that is the reason why we need to dynamically allocate widgets on the heap (using pointers and new).
    Last edited by Zlatomir; 11th May 2010 at 11:59.

  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: What owns what?

    Quote Originally Posted by cass View Post
    But what happens to root QWidgets?
    Nothing happens to them.
    What happens if you add a QWidget to a QScene?
    Does the QScene now owns the QWidget or does the QGraphicsViewProxy now owns the QWidget?
    Reading the docs from time to time, really doesn't hurt.
    Quote Originally Posted by QGraphicsProxyWidget docs
    QGraphicsProxyWidget shares ownership with QWidget, so if either of the two widgets are destroyed, the other widget will be automatically destroyed as well.
    What owns the QGraphicsViewProxy objects that is returned by scene->addWidget(new_widget)?
    There is no such class in Qt currently but if you mean QGraphicsProxyWidget then it's an item like any other so the same rules that apply to other items apply to this item class as well.
    Will a widget take ownership of a layout?
    Yes, the layout is reparented to the widget it controls.
    E.g. what happens with root_widget->setLayout(new_layout)?
    What do you mean "what happens"? The world will not explode, I can tell you that

    Will the layout take ownership if I add a widget/layout to a root layout, e.g. root_layout->addWidget(new_widget) or root_layout->addLayout(new_sub_layout)?
    Layout doesn't own any widgets.

    If I have an application that dynamically creates objects and setup QT signal connections between the events with QObject::connect(), is it safe to assume that the connections will automatically be deleted when either one of the objects they used to connect gets deleted?
    Yes.

    You know, all of these questions could have been answered by writing a simple test application and it would take less time than asking those questions here...
    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 2 users say thank you to wysota for this useful post:

    cass (12th May 2010), Zlatomir (12th May 2010)

Tags for this Thread

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.