Results 1 to 6 of 6

Thread: Ownership in a QWidget

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Ownership in a QWidget

    A quick question:

    QWidget::setLayout takes ownership of the layout and QLayout::addWidget takes ownership of the passed widgets. How does it know when to delete an object and when not? Or: why does this work:

    Qt Code:
    1. class test : public QWidget
    2. {
    3. public:
    4. test()
    5. {
    6. l->addWidget(&bar);
    7. setLayout(l);
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 


    Why doesn't a double free occur, once when the layout discards the scrollbar and once when the test dtor?

  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: Ownership in a QWidget

    The destructor of test is executed before the destructor of QWidget so bar gets deleted (and removed from the layout) before QWidget destructor has a chance to delete the layout.
    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
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ownership in a QWidget

    Ah I understand now. Thank you. Is this a common pattern or is it better to allocate one at runtime with "new QScrollBar()" ?

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Ownership in a QWidget

    Well, it "works": On destroying test you get orphaned QHBoxLayout with an invalid item bar (now deleted). The QHBoxLayout needs to specify a parent
    Qt Code:
    1. QHBoxLayout *l = new QHBoxLayout(this);
    To copy to clipboard, switch view to plain text mode 
    or l needs to be a data item of test and test needs a dtor which will delete l. Naturally, specifying the parent is a way simpler and cleaner.

  5. #5
    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: Ownership in a QWidget

    Quote Originally Posted by Radek View Post
    Well, it "works": On destroying test you get orphaned QHBoxLayout with an invalid item bar (now deleted). The QHBoxLayout needs to specify a parent
    Qt Code:
    1. QHBoxLayout *l = new QHBoxLayout(this);
    To copy to clipboard, switch view to plain text mode 
    or l needs to be a data item of test and test needs a dtor which will delete l.
    setLayout() does an implicit setParent() of the layout to the widget so the layout will be destroyed when the widget it manages is destroyed.
    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.


  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Ownership in a QWidget

    Quote Originally Posted by tuli View Post
    Ah I understand now. Thank you. Is this a common pattern or is it better to allocate one at runtime with "new QScrollBar()" ?
    Child widgets are usually allocated on the heap.
    Keeps the headers clean from includes that are internals to the parent widget.

    Cheers,
    _

Similar Threads

  1. ownership of QMenu after calling addMenu()
    By cic in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2013, 17:27
  2. QComboBox setModel - ownership???
    By lotek in forum Newbie
    Replies: 3
    Last Post: 5th August 2011, 15:37
  3. QTableView and QStyledItemDelegate ownership
    By lxman in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2011, 05:19
  4. Replies: 3
    Last Post: 14th May 2010, 23:00
  5. Thread Ownership Problem
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 00:18

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.