3

just a quick question regarding parent and child relationship. I've been reading tutorials but i can't seem to find any tutorials or readings that demonstrates proper placement of the "this" keyword.

Here is my code:

window::window(){


   bar = new QProgressBar;
   button = new QPushButton;

   bar->setTextVisible(false);
   button->setText("IRON LOADER");                   //widget

   QGridLayout * GL = new QGridLayout();             //layout
   GL->addWidget(bar);
   GL->addWidget(button);
   QGroupBox * gb = new QGroupBox("Group",this);     //widget



   //second box

   QTextEdit * tester = new QTextEdit();            //widget
   tester->setFixedHeight(100);
   QVBoxLayout * ly = new QVBoxLayout();            //layout
   ly->addWidget(tester);
   QWidget * widge = new QWidget(this);             //widget



                                //final
   widge->setLayout(ly);        //first widget group
    gb->setLayout(GL);          //second widget group

                                //main wrapper
    QGridLayout * test = new QGridLayout;
    test->addWidget(widge,0,1);
    test->addWidget(gb,0,0);
    //set up



    QWidget * central = new QWidget;
    central->setLayout(test);

    setCentralWidget(central);      //set central

}

My program is just simple:

it consist of 3 files, namely: window.h, main.cpp and window.cpp

and from the windows.cpp,i inherited QMainWindow and then i implemented 2 group of widgets:

the left one consist of a QPushButton and a QProgressBar . The right one has a QTextEdit.

My code compiles and works as intended.

One thing that confuse me is this: when should i put this? is it alright to not put the this keyword on some of them(since by the time the parent gets deleted is the same as closing the program) ?

I read that parent / child relationship on qt is about deletion and memory management.

Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49
  • I am asking when to supply the "this" pointer for the QParent constructors. Not the keyword "this". – Carlos Miguel Colanta Aug 05 '15 at 07:11
  • This is relative to Qt and it has a different meaning – Marco A. Aug 05 '15 at 07:11
  • When passing parent as argument to widget it means when parent set free the child be free too. You free mainWindow in main.cpp so widge will be free simultaneously. – aLoneStrider Aug 05 '15 at 07:12
  • If you inherit from QMainWIndow, you probably want your constructor to look a bit like this: window::window() : QMainWindow() {...} This ensures that the main window is properly constructed. It's probably not important in this case, but once you start dealing with more complicated setups, you will need it. It's also worth to always take a QWidget* as input, as in declaring a default value, window(QWidget* parent=0); and then defining the constructor as window::window(QWidget* parent) : QMainWindow(parent) {...} – Jaciq Aug 05 '15 at 07:24

1 Answers1

3

If a program leaks but it is terminated, the OS will reclaim its memory (the OS tracks the memory allocations performed). Anyway it is not a good practice to leak memory for the sake of it since your program might be extended by someone else or used differently than previously expected.

Regarding Qt the widgets tutorial explains the intended use of this

As with QObjects, QWidgets can be created with parent objects to indicate ownership, ensuring that objects are deleted when they are no longer used. With widgets, these parent-child relationships have an additional meaning: each child widget is displayed within the screen area occupied by its parent widget. This means that when you delete a window widget, all the child widgets it contains are also deleted.

To answer your question: it is both good practice for memory management and for display correctness to set up parent-child relationships between widgets.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • I see, so in short: i should utilize the parenting whenever i can to avoid memory leaks aka always make the mainwindow the parent. Would that be a good assumption? – Carlos Miguel Colanta Aug 05 '15 at 07:15
  • You should if those widgets are intended to be direct children of your window. It's always better to follow best practices rather than relying on a particular 'hacky' case. – Marco A. Aug 05 '15 at 07:16
  • Alright just one more thing if the structure of my UI is something like this : `mainwindow -> widget -> layout -> widget -> layout->widget`. who is the parent of the youngest widget? – Carlos Miguel Colanta Aug 05 '15 at 07:18
  • Layouts don't act as parents. When you add widgets to layouts, the layout will [automatically reparent the widget to the layouts parent](https://github.com/Vitallium/qt5/blob/master/qtbase/src/widgets/kernel/qlayout.cpp#L868). – thuga Aug 05 '15 at 07:42