Results 1 to 19 of 19

Thread: Parent child relationship in Qt

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Parent child relationship in Qt

    Every widget has a parent which helps in cases of deletion, i.e. when the parent gets deleted, the child gets automatically thrown out to /dev/null.

    Now I see QWidget *parent being passed to the user defined class's constructor here, in this link: http://doc.qt.nokia.com/4.7/tutorial...ook-part1.html
    Qt Code:
    1. class AddressBook : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AddressBook(QWidget *parent = 0);
    7.  
    8. private:
    9. QLineEdit *nameLine;
    10. QTextEdit *addressText;
    11. };
    To copy to clipboard, switch view to plain text mode 
    What does this indicate? In what way is this useful?

  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: Parent child relationship in Qt

    When you create the object (instance of your class) you can pass a QWidget pointer (the address of the parent).
    Example
    Qt Code:
    1. AddressBook *myAddressBook = new AddressBook (pointerToTheParent);
    To copy to clipboard, switch view to plain text mode 
    Or if you don't pass a pointer to parent the object will have no parent (that is what the = 0 means: the parent pointer is NULL) and if the object doesn't get a parent (like when you add it to a layout it gets parented automatically) you will need to delete it (only if allocated on the heap).

    Hope i managed to clear things a little bit

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

    TheIndependentAquarius (10th May 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Parent child relationship in Qt

    Thanks for responding, but situation is still not crystal clear. Now since I have mentioned the parent's pointer in the 'AddressBook''s constructor, won't I have to manually delete it, if I allocate it with new?

    What are the other uses of mentioning the parent pointer?

  5. #4
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    If you mentioned the parent, then when you delete your parent the child will be deleted with it...

  6. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Parent child relationship in Qt

    I know that, my in the OP the parent is QWidget itself. How should I delete it?

  7. #6
    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: Parent child relationship in Qt

    Quote Originally Posted by Anisha Kaul View Post
    Thanks for responding, but situation is still not crystal clear. Now since I have mentioned the parent's pointer in the 'AddressBook''s constructor, won't I have to manually delete it, if I allocate it with new?
    If it doesn't have a parent you need to delete it.
    If it has a parent - the parent will delete it, you need to make sure that in parent-child "chain" the "parent of everything" gets deleted (either manually <delete call> or by creating it on stack)
    Quote Originally Posted by Anisha Kaul View Post
    What are the other uses of mentioning the parent pointer?
    Memory management, the parent show/hide it's children widgets (if they are not a new window - or explicitly hidden by programmer), the layout system uses the parent-child relationship, i'm sure that there are many other things that i might forgot - you need to practice with C++ and Qt and you will discover and learn them.

    This documentation page has links to basic "patterns" used with Qt - as i said i'm sure there are other places where parent-child relationship is useful.

  8. The following user says thank you to Zlatomir for this useful post:

    TheIndependentAquarius (10th May 2011)

  9. #7
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    If u have AddressBook(QWidget *parent = 0),
    then by default the parent is the one in which u have created the addressbook
    Last edited by Archa4; 10th May 2011 at 13:20.

  10. #8
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Parent child relationship in Qt

    Quote Originally Posted by Zlatomir View Post
    If it has a parent - the parent will delete it, you need to make sure that in parent-child "chain" the "parent of everything" gets deleted (either manually <delete call> or by creating it on stack)
    Yeah, and in this case, "QWidget" class is itself the parent, so you mean to say that I need to delete the QWidget pointer, to get the addressbook object deleted?

    If yes, then what's the way to delete the QWidget pointer which I haven't initialized even?

    Quote Originally Posted by Zlatomir View Post
    the parent show/hide it's children widgets (if they are not a new window - or explicitly hidden by programmer), the layout system uses the parent-child relationship,
    Thanks, I'll read about "show/hide" thing, this link is what you were referring to: http://doc.qt.nokia.com/4.7/objecttrees.html ? That seems to be helpful, I'll go through it.
    Last edited by TheIndependentAquarius; 10th May 2011 at 13:19.

  11. #9
    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: Parent child relationship in Qt

    Quote Originally Posted by Anisha Kaul View Post
    Yeah, and in this case, "QWidget" class is itself the parent, so you mean to say that I need to delete the QWidget pointer, to get the addressbook object deleted?
    You don't delete the QWidget pointer from your class definition.

    You just need to make sure the parent, if any, gets deleted (the parent is passed when you construct the object)
    Ok, example code:
    Qt Code:
    1. #include <QtGui>
    2. #include "addressbook.h"
    3. //...every needed header
    4.  
    5. int main(...) {
    6. //QApplication app ....
    7. QWidget *parent = new QWidget; //a parent for your addressbook
    8. AddressBook *myAddressBook = new AddressBook(parent); //pass a pointer to parent widget when you construct an AddressBook object
    9. //... other code, maybe some layout if necessary...
    10. parent->show(); //will show the chidren widgets too
    11.  
    12. int returnValue = app.exec();
    13. delete parent; //this will delete the parent children too
    14. return returnValue;
    15. }
    To copy to clipboard, switch view to plain text mode 
    or you can create the parent on the stack and you don't have to call delete:
    Qt Code:
    1. int main(...) {
    2. //QApplication app ....
    3. QWidget parent; //a parent for your addressbook - parent is created on stack
    4. AddressBook *myAddressBook = new AddressBook(&parent); //pass a pointer to parent widget when you construct an AddressBook object
    5. //... other code, maybe some layout if necessary...
    6. parent.show(); //will show the chidren widgets too
    7.  
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to Zlatomir for this useful post:

    TheIndependentAquarius (11th May 2011)

  13. #10
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Parent child relationship in Qt

    I think you may be confusing several things here --

    First off, in this example AddressBook is a subclass of QWidget. This has nothing to do with parent/child relationships (other than the fact that the implementation of AddressBook's constructor needs to be sure to cascade the "parent" pointer to QWidget's constructor). The parent/child relationship and the subclass/superclass relationship are entirely separate and distinct things.

    Second, the idiom "(SomeClass* someParameter = 0)" defines an "optional" parameter. That is, the "= 0" means that if you don't code the parameter (ie, in this case just code "AddressBook()" for the constructor) then it's as if you had coded "AddressBook(NULL)", passing a null value for the parent.

    Third, the parent-child relationship is used for several things. For all QObjects (not just QWidgets) there's the automatic destruction function. For all QWidgets there's a relationship in terms of how graphics are built and displayed -- children paint on their parents, hiding a parent hides the children, etc. For many parent-child relationsips (mostly between QWidgets) events of certain types (particularly key and touch events) are propagate to the parent if the child doesn't handle them.

  14. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Parent child relationship in Qt

    Quote Originally Posted by Archa4 View Post
    If u have AddressBook(QWidget *parent = 0),
    then by default the parent is the one in which u have created the addressbook
    No thats wrong. If you don't pass a parameter then the widget has no parent and you need to delete it yourself.

  15. #12
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    Hi. I have same question with a little difference. I build a form with tab widget. After running, i make it unvisible, and call with another button click. On close the child form is deleting with own objects. My problem is, with tab widget. Because i call it with reference, so on child form close its deleting from heap. After that if i click the button second time, its crashing. I cant copy my existed tab widget, or cant create a copy. How can i do it ?

    Here is my dirty code

    Qt Code:
    1. void MainWindow::on_action_Config_triggered()
    2. {
    3. confw = new QDialog(this);
    4. confw->setModal(true);
    5. confw->setSizeGripEnabled(false);
    6.  
    7. confw->setObjectName("confwid");
    8. confw->setWindowTitle("Configuration");
    9.  
    10. QWidget *tab = new QWidget;
    11. tab = (QWidget *)ui->tab1; //existing tab widget object, which i created with qt creator for another mainwindow object
    12. tab->setParent(confw); // on confw delete, its deleting too. because i cant make it copy!!! :(
    13.  
    14. tab->show();
    15. confw->setMaximumSize((tab->width()+10),(tab->height()+10));
    16. confw->setMinimumSize((tab->width()+10),(tab->height()+10));
    17. ui->pushButton->setVisible(false);
    18. confw->resize(tab->size().width()+10,tab->size().height()+10);
    19. confw->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,confw->size(),
    20. qApp->desktop()->availableGeometry()));
    21.  
    22. QPushButton *pushbutton = new QPushButton(ui->groupBox_6);
    23. pushbutton->setText("Save");
    24. pushbutton->setGeometry(ui->pushButton->geometry());
    25. pushbutton->setIcon(ui->pushButton->icon());
    26. connect(pushbutton,SIGNAL(clicked()),this,SLOT(configcontrol()));
    27. connect(pushbutton,SIGNAL(clicked()),confw,SLOT(close()));
    28. ui->groupBox_6->setVisible(true);
    29. ui->groupBox_6->setEnabled(true);
    30. pushbutton->setVisible(true);
    31. pushbutton->setEnabled(true);
    32. pushbutton->show();
    33. confw->showNormal();
    34. }
    To copy to clipboard, switch view to plain text mode 

    Regards.

  16. #13
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    Any suggestions ?

  17. #14
    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: Parent child relationship in Qt

    create confw as class member variable and don't delete it, just close and show it as and when required.

    and moreover looks like you are complicating the situation here, I was not able to understand what you are trying to do, if you could explain how you want things to displayed I can suggest something simple

  18. #15
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    My english is really bad. Pardon me.
    Thanks Santosh There is just tab object i have. i didnt want to make a new one. After my application run, i m controlling the user input datas. if there is no data in registry, user can input the data by using this tab and tab objects(lineedits, etc.). else if they are recorded in the registry, then removing tab1 object from tabwidget, so i hide it. If user wants to change these datas, just need to click "config" action in menubar object. And then my goal is starting . As you said, i didnt delete confw object. But its making memory leak! i just want to copy my tab1 object.

    confw object is dynamic, but ui->tab1 object is static. tab1 object creating for just one (1) time on application running. But confw object is creating at every "config action" triggered. So, i need to make tab1 object as dynamic.

    i can make a new tab object like "tab1" with creating all components again one by one. But i didnt want to do it. I just want to use existing tab.

    Wish i can explain myself.

    Thanks.
    Last edited by kosasker; 13th July 2011 at 12:14.

  19. #16
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Parent child relationship in Qt

    [Someone turned the posts order upside down on me]

  20. #17
    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: Parent child relationship in Qt

    make confw and ui->tab1 static, add confw to tab widget as a page dynamically, and hide / show dynamically in on_action_Config_triggered()

    You may need to check you logic again, you wanted to add a page in tab widget, then why do you create QDialog in on_action_Config_triggered()?

    1. Do you want to show configuration widget / form in tab widget as a page?
    or
    2. Do you want to show confguration widget / form as dialog?

    this what you code does (as posted above)
    1. Creates a configuration QDialog (confw)
    2. Adds a new QWidget (tab)
    3. assign ui->tab1 to tab, the QWidget created in step 2 is lost, this is memory leak - Why do you do this?

    You are mixing tab widget & QDialog, I hope you are getting my point

  21. The following user says thank you to Santosh Reddy for this useful post:

    kosasker (13th July 2011)

  22. #18
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    Thanks for answer again. After creation confw, i were closed it, when my job is completed. Not hiding

    i' m making show & close... Not show&hide.... Difference is that. Thank you. i will hide it... why i never think about it? i always closing or destroy my forms..
    Thanks again

  23. #19
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Parent child relationship in Qt

    Hi. Sorry for late. My solution is here.

    Qt Code:
    1. void MainWindow::on_action_Config_triggered()
    2. {
    3. QDialog *dia = this->findChild<QDialog *>("confwid"); // with this code, i' m finding object from object name
    4. if (dia == 0) // if object confwid is not exist, value is "0" (zero), then i 'm creating object.
    5. {
    6. confw = new QDialog(this);
    7. confw->setModal(true);
    8. confw->setSizeGripEnabled(false);
    9.  
    10. confw->setObjectName("confwid"); // object name is here :)
    11. confw->setWindowTitle("Configuration");
    12.  
    13. QWidget *tab = new QWidget;
    14. tab = (QWidget *)ui->tab1; //existing tab widget object, which i created with qt creator for another mainwindow object
    15. tab->setParent(confw);
    16.  
    17. tab->show();
    18. confw->setMaximumSize((tab->width()+10),(tab->height()+10));
    19. confw->setMinimumSize((tab->width()+10),(tab->height()+10));
    20. ui->pushButton->setVisible(false);
    21. confw->resize(tab->size().width()+10,tab->size().height()+10);
    22. confw->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,confw->size(),
    23. qApp->desktop()->availableGeometry()));
    24.  
    25. QPushButton *pushbutton = new QPushButton(ui->groupBox_6);
    26. pushbutton->setText("Save");
    27. pushbutton->setGeometry(ui->pushButton->geometry());
    28. pushbutton->setIcon(ui->pushButton->icon());
    29. connect(pushbutton,SIGNAL(clicked()),this,SLOT(configcontrol()));
    30. connect(pushbutton,SIGNAL(clicked()),confw,SLOT(hide())); // with pushbutton clicked, i' m hiding QDialog. Not close!
    31. ui->groupBox_6->setVisible(true);
    32. ui->groupBox_6->setEnabled(true);
    33. pushbutton->setVisible(true);
    34. pushbutton->setEnabled(true);
    35. pushbutton->show();
    36. confw->showNormal();
    37. }
    38.  
    39. else // if confwid named object is avaible, i just setting geometry and setting visibilty true for showing.
    40. {
    41. this->confw->setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter,confw->size(),
    42. qApp->desktop()->availableGeometry()));
    43. this->confw->setVisible(true);
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    QObject::findChild

    Its working clearly. That' s it. Thanks.
    Last edited by kosasker; 14th July 2011 at 08:51.

Similar Threads

  1. Parent/child transformations
    By jano_alex_es in forum Newbie
    Replies: 4
    Last Post: 5th November 2009, 10:13
  2. Parent window over child
    By neolol in forum Qt Programming
    Replies: 4
    Last Post: 15th May 2009, 21:15
  3. Parent Child for window
    By febil in forum Qt Programming
    Replies: 6
    Last Post: 1st April 2009, 05:00
  4. resizeEvent from parent to child?
    By ChasW in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2007, 18:21
  5. Parent-child-problems ;)
    By nupul in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2006, 14:03

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.