Question about qt objects deletion
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
Code:
{
Q_OBJECT
public:
Myform()
{
}
~Myform()
{
}
private:
};
Re: Question about qt objects deletion
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.
Re: Question about qt objects deletion
can you please modify the class and show me what do you mean with " pass this pointer to QWidget constructor " and thanks in advance
Re: Question about qt objects deletion
Btw, you class already deletes its children, this modification is for making instance of your class being deleted by their parents.
Cheers,
_
Re: Question about qt objects deletion
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:
Re: Question about qt objects deletion
Quote:
Originally Posted by
MI
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.
Quote:
Originally Posted by
MI
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,
_
Re: Question about qt objects deletion
new question : by doing this " MyForm(QWidget *parent = 0) : QWidget(parent) " what are we really saying to the compiler ...
and please putup with me
Re: Question about qt objects deletion
Quote:
Originally Posted by
MI
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.
Re: Question about qt objects deletion
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,
_
Re: Question about qt objects deletion
Big thanks to every one who helped me :)