Results 1 to 7 of 7

Thread: How to save Child if Parent Object is deleted.

  1. #1
    Join Date
    Mar 2009
    Location
    delhi India
    Posts
    56
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default How to save Child if Parent Object is deleted.

    Hi,

    I'm trying to maintain strong references to Qt objects in my application.

    Given the following code snippet:

    // ...
    QWidget bar;
    QLabel* foo = new QLabel(&bar);
    // ...

    'bar' now owns 'foo' and is responsible for deleting it. Now I know that the following is valid:

    // ...
    QWidget bar;
    QLabel* foo = new QLabel(&bar);
    delete foo;
    // ...

    Because in 'foo's destructor it removes itself from its parent's to-delete list.

    But the problem is that - given a parent's child was not deleted - whenever the parent is destroyed it destroys its children and any external references to the children in the application become dangling pointers.

    So what I need is something that tells the parent that there are still external references to the children, so it should not delete them. Since widgets don't inherit from QSharedData, I have no idea how to do this.

    Not setting parents is not an option, because in that case, widgets don't appear inside other widgets, since there is no relation between them.

    Please help me to find a solution to this.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to save Child if Parent Object is deleted.

    Why do you maintain the child widget's pointer when the parent no more exists?

  3. #3
    Join Date
    Mar 2009
    Location
    delhi India
    Posts
    56
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to save Child if Parent Object is deleted.

    That is required in my application.Where those references to child in mainted in some other class.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to save Child if Parent Object is deleted.

    Do the the references maintained in some other class need to be QWidget type? if not then you may think of having a Class whose reference / pointer in maintained by you, and then connect to this class to the child QWidget. This way even if the child widget is deleted, you still have the your class object, with which you can work.

    Just as a suggestion:
    As I understand, you may be trying to have a custom widget, with some custom data in it, and when the widget is deleted, you lose you data in there. It is better to keep you data components and GUI components separated, mixing them them in a class will give complications in long run, as the program grows

  5. #5
    Join Date
    Mar 2009
    Location
    delhi India
    Posts
    56
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to save Child if Parent Object is deleted.

    I didn't completely get you but my problem is excatly the same as told by you .

    I am Adding some QWidget in my QFiledialog's layout.
    When my QFileDialog is deleted it delets that Qwidget which i am using in my application.

    Could you detail me more on my problem.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to save Child if Parent Object is deleted.

    So, you may need to create another widget for the QFileDialog, and not use the one used by application. Also as required by your logic, you may transfer data between the application widget and QFileDialog widget

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to save Child if Parent Object is deleted.

    If you really want to do it your way, then reimplement the QFileDialog destructor, and call yourSharedWidget->setParent(NULL); there, this way the widget will be removed from children list, and not deleted. But I agree with Santosh Reddy, maybe you should rethink the design.

Similar Threads

  1. Parent child relationship in Qt
    By TheIndependentAquarius in forum Newbie
    Replies: 18
    Last Post: 14th July 2011, 08:43
  2. Parent/child transformations
    By jano_alex_es in forum Newbie
    Replies: 4
    Last Post: 5th November 2009, 10:13
  3. Parent Child for window
    By febil in forum Qt Programming
    Replies: 6
    Last Post: 1st April 2009, 05:00
  4. Connecting child and parent
    By maverick_pol in forum Qt Programming
    Replies: 17
    Last Post: 17th July 2007, 08:11
  5. resizeEvent from parent to child?
    By ChasW in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2007, 18:21

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.