giblit
4th March 2014, 02:52
Say we have a custom Widget that looks something along the lines of:
class Test : public QWidget
{
public:
Test(QWidget *parent = nullptr) : QWidget(parent) , label(new QLabel(tr("Hello")))
{
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget( label );
setLayout( layout ); //add the current layout and widgets to be children to this(Test)
}
~Test(void){}
private:
QLabel *label;
};
What I am concerned about is memory leaking from label or layout when I delete a parent (Test) object. I tried something like:
~Test( void ) { if( label ) std::cerr << "Label was not destroyed!" << std::endl; } and the output was not what I was hoping for could this be due to me mistaking what they mean by being a parent/child or does it delete them in some random order?
Also when we have nested layouts and we destroy the parent layout are the children layouts automatically deleted also?
Something like:
QHBoxLayout *layout1 = new QHBoxLayout;
QVBoxLayout *layout2 = new QVBoxLayout;
layout2->addLayout(layout1);
//if layout2 is deleted is layout1 also?
I am used to having to manually delete heap stuff or the use of RAII not the object tree way. Thanks for any help I greatly appreciate it. :)
class Test : public QWidget
{
public:
Test(QWidget *parent = nullptr) : QWidget(parent) , label(new QLabel(tr("Hello")))
{
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget( label );
setLayout( layout ); //add the current layout and widgets to be children to this(Test)
}
~Test(void){}
private:
QLabel *label;
};
What I am concerned about is memory leaking from label or layout when I delete a parent (Test) object. I tried something like:
~Test( void ) { if( label ) std::cerr << "Label was not destroyed!" << std::endl; } and the output was not what I was hoping for could this be due to me mistaking what they mean by being a parent/child or does it delete them in some random order?
Also when we have nested layouts and we destroy the parent layout are the children layouts automatically deleted also?
Something like:
QHBoxLayout *layout1 = new QHBoxLayout;
QVBoxLayout *layout2 = new QVBoxLayout;
layout2->addLayout(layout1);
//if layout2 is deleted is layout1 also?
I am used to having to manually delete heap stuff or the use of RAII not the object tree way. Thanks for any help I greatly appreciate it. :)