Results 1 to 2 of 2

Thread: Reparenting a QObject

  1. #1
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Reparenting a QObject

    Hi there,

    I created a QObject based class passing another QObject as parent. Now I want to use the QObject somewhere else and need to reparent it. So far I use
    Qt Code:
    1. // first
    2. MyClass * c1 = new MyClass(containerWidget);
    3. containerWidget->widgetList.append(c1);
    4.  
    5. // later
    6. MyClass * c2 = containerWidget->widgetList->takeAt(14);
    7. c2->setParent(newContainerWidget);
    To copy to clipboard, switch view to plain text mode 

    Is that enough? What happens, if containerWidget gets destroyed before newContainerWidget? Will it delete my class instance as well or does the setParent() call sever the parent-children connection completely?

    Bye
    Andreas

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reparenting a QObject

    Yes, it will work like this. Once you reparent the object, the old parent won't have anything to do with your object.

    But make sure to disconnect any signals/slots you have set between the child and the old parent and connect them to the new parent.

  3. The following user says thank you to marcel for this useful post:

    ghorwin (13th April 2007)

Similar Threads

  1. Reparenting
    By brcain in forum Qt Tools
    Replies: 15
    Last Post: 5th September 2020, 19:41
  2. From QObject to QWidget
    By Maxilys in forum Qt Programming
    Replies: 3
    Last Post: 2nd February 2014, 18:07
  3. Interface composition and QObject
    By brcain in forum Qt Programming
    Replies: 9
    Last Post: 20th November 2006, 18:56
  4. Problem with Destructor of QObject
    By mikro in forum Qt Programming
    Replies: 5
    Last Post: 9th July 2006, 20:05
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 18:49

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.