Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[solved]Why can't declare an object layout management, must be a pointer?
joeQ last edited by joeQ
when we use the layout to manage some buttons ,such as:
QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(btnfirst); layout->addWidget(btnsecond); layout->addWidget(btnthree); layout->addWidget(btnself); layout->addWidget(btnexit); QWidget *p = new QWidget; p->setLayout(layout);'''
that is right,but the following is wrong ,why?
QVBoxLayout layout; layout.addWidget(btnfirst); layout.addWidget(btnsecond); layout.addWidget(btnthree); layout.addWidget(btnself); layout.addWidget(btnexit); QWidget *p = new QWidget; p->setLayout(&layout);
I don't know why , we must be use a pointer ? (thank you!)
mcosta last edited by mcosta
Hi and welcome to devnet,
QObjectinstance has an array of pointers of its children; it uses that list to manage memory (destroys the children in the destructor) and visibility (hides the children when is hidden, resizes them according to layouts, ....).
Your code doesn't work because at the end of the function, the
layoutvariable goes out of scope and is destroyed; so the pointer you passed to
joeQ last edited by
@mcosta thank you very much! I get it.