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);

2 Answers 2

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.

2
  • 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? May 3, 2011 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, 2012 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.