2

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);
Community
  • 1
  • 1
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

7

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.

Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • Very much thankful, `button1 = new QPushButton("Button1", this);` this line was in a my defined class, so this means that my class is the parent of the pushbutton in this case! Is my understanding correct? – Aquarius_Girl May 03 '11 at 18:08
  • So does that every widget accepts a qwidget pointer to set it to it's parent? I'm newbie – Mr.Anubis Aug 29 '12 at 12:28
-1

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

Amedio
  • 895
  • 6
  • 13