PDA

View Full Version : destroing nested layout



mastupristi
26th November 2009, 13:44
Suppose I have the following code

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QDialog>

class MainWindow : public QDialog
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};

#endif // MAINWINDOW_H


MainWindow.cpp

#include "mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
: QDialog(parent)
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
QHBoxLayout *slaveLayout = new QVBoxLayout(this);

slaveLayout->addWidget(new QLabel("A0"));
slaveLayout->addWidget(new QLabel("A1"));

mainLayout->addLayout(slaveLayout);
mainLayout->addWidget(new QLabel("B0"));

setLayout(mainLayout);
}

MainWindow::~MainWindow()
{
delete layout();
}


does the destrictor delete both mainLayout and slaveLayout?
Or do I need to modify as follows:


MainWindow::~MainWindow()
{
delete layout()->layout();
delete layout();
}


thanks

Lykurg
26th November 2009, 14:34
does the destrictor delete both mainLayout and slaveLayout?

Yes. You have to do nothing.

mastupristi
30th November 2009, 09:58
Yes. You have to do nothing.Ok, that's fine :)

Now I wonder if widgets are deleted too.

If I have the following contructor:

#include "mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
: QDialog(parent)
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
QHBoxLayout *slaveLayout = new QVBoxLayout(this);

static QLabel A0("A0");
slaveLayout->addWidget(&A0);
slaveLayout->addWidget(new QLabel("A1"));

mainLayout->addLayout(slaveLayout);
mainLayout->addWidget(new QLabel("B0"));

setLayout(mainLayout);
}


and desctructor:

MainWindow::~MainWindow()
{
delete layout();
}

does the destructor destroy also QLabel A0, "A1" and "B0"? I wonder if "A1" and "B0" are deallocated, and what about A0 that is statically allocated

thanks