PDA

View Full Version : Question about qt objects deletion



MI
14th February 2014, 09:23
Hello everyone i am a newbie to this form and to qt world and i have a simple question which is how to make my class auto deletes its members by accepting a parent


class Myform : public QWidget
{
Q_OBJECT

public:

Myform()
{
b=new QPushButton("OK",this);
l=new QLineEdit(this);
mylabel=new QLabel(this);
}

~Myform()
{

}

private:

QPushButton *b;
QLineEdit *l;
QLabel *mylabel;

};

wysota
14th February 2014, 09:45
Just like you do in lines 9-11 -- pass the widget pointer as the last argument to every child. Similarily you can modify your constructor to accept a QObject pointer and call the base class implementation where you will pass this pointer to QWidget constructor.

MI
14th February 2014, 10:01
can you please modify the class and show me what do you mean with " pass this pointer to QWidget constructor " and thanks in advance

anda_skoa
14th February 2014, 11:50
MyForm(QWidget *parent = 0) : QWidget(parent)
{
...
}


Btw, you class already deletes its children, this modification is for making instance of your class being deleted by their parents.

Cheers,
_

MI
14th February 2014, 11:57
MyForm(QWidget *parent = 0) : QWidget(parent) i sow this expression many times and i didn't get it ....can you explaine please
and what do you mean by the class already deletes its children
and last i am sorry about those too many questions i am a confused newbie:confused:

anda_skoa
14th February 2014, 14:11
MyForm(QWidget *parent = 0) : QWidget(parent) i sow this expression many times and i didn't get it ....can you explaine please

This is a constructor of a C++ class called MyForm.
It takes one argument of type QWidget*, named parent, and has a default value of 0.
The parent argument is passed on to the base class constructor.



and what do you mean by the class already deletes its children

Because you are passing "this" as the parent of the children. QObject subclasses such as QWidget and thus MyForm delete their children when they are deleted.

Cheers,
_

MI
14th February 2014, 14:43
new question : by doing this " MyForm(QWidget *parent = 0) : QWidget(parent) " what are we really saying to the compiler ...
and please putup with me

sulliwk06
14th February 2014, 15:13
new question : by doing this " MyForm(QWidget *parent = 0) : QWidget(parent) " what are we really saying to the compiler ...
and please putup with me
Whatever widget you create an instance of this form in, when you call "MyForm myForm(this);" You are saying that the widget you called it from is the parent of myForm. When that parent gets deleted, it will call the destructor on all of its children causing a chain reaction.

anda_skoa
14th February 2014, 15:30
sulliwk06 already explained the runtime behavior created by this so here the "what are we saying to the compiler" bit:

we tell the compiler that instance of class MyForm can be constructed with zero or one argument of type QWidget*.

If no argument is passed to the constructor, then a null pointer should be assumed.

The given value (either the argument or the null pointer) should be uses as the argument of the invocation of the base class constructor.

Cheers,
_

MI
14th February 2014, 20:00
Big thanks to every one who helped me :)