Qt: Program is crashing when deleting central widget of QMainWindow in destructor : <p>I am learning Qt. Currently I stuck at layout stuff for <code>QMainWindow</code>. As suggested in some of examples available on internet i used <code>QWidget</code> to be used in <code>setCentralWidget(QWidget*)</code> method of QMainWindow. But as part of clean up when i am deleting central <code>QWidget</code> the program is crashing.</p>

<p>here is the code for .h</p>

<pre><code>class MyMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MyMainWindow(QWidget *parent = 0);
    ~MyMainWindow();

private:
    QVBoxLayout m_p1_level_vbox;
    QHBoxLayout m_p2_level_vbox;
    QHBoxLayout m_p2_level_hbox;
    QWidget* m_central_widget;
    QPushButton* m_increase_pressure;
    QPushButton* m_decrease_pressure;
};
</code></pre>

<p>for .cpp</p>

<pre><code>MyMainWindow::MyMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_central_widget(new QWidget(this))
    , m_increase_pressure(new QPushButton("+", this))
    , m_decrease_pressure(new QPushButton("-", this))
{
    m_p2_level_hbox.addWidget(m_increase_pressure);
    m_p2_level_hbox.addWidget(m_decrease_pressure);

    m_p1_level_vbox.addLayout(&amp;m_p2_level_hbox);

    m_central_widget-&gt;setLayout(&amp;m_p1_level_vbox);
    setCentralWidget(m_central_widget);
}

MyMainWindow::~MyMainWindow()
{
    delete m_central_widget; // commenting this line doesn't  crash the program
    delete m_increase_pressure;
    delete m_decrease_pressure;
}
</code></pre>

<p>main.cpp</p>

<pre><code>int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyMainWindow w;
    w.show();
    return a.exec();
}
</code></pre>

<p>In destructor if i don't delete <code>m_central_widget</code> then it works.</p>

<p>What i am doing wrong here?</p>
