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);
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 this
refers 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.
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
精彩评论