Aquarius_Girl
Aquarius_Girl

Reputation: 22946

Parent passed to QPushButton's constructor

I have been following this thread: Memory management in Qt?

QPushButton::QPushButton ( const QString & text, QWidget * parent = 0 )

So, in an example I saw the following way of creating push button's object. My concern is the second parameter, "parent", a this pointer has been passed there, does it mean that this widget is its own parent? I know I am missing a point, please point it out.

button1 = new QPushButton("Button1", this);

Upvotes: 2

Views: 1741

Answers (2)

Jérôme
Jérôme

Reputation: 27047

Be careful, this does not refer to the QPushButton.

This line of code :

button1 = new QPushButton("Button1", this);

is probably part of a QWidget-based class, and that's the one thisrefers to !

That means the QWidget-based class is the owner of the QPushButton it is displaying.

It also means that when the instance of the QWidget-based class is deleted, it will delete all its children elements, which means the QPushButton button1 will be deleted as well, automatically.

Upvotes: 7

Amedio
Amedio

Reputation: 893

Yes the parent you set here is the widget you're, you use parent parameter on controls of almost any GUI Framework to know where the control is.

See you

Upvotes: -1

Related Questions