Qt Parent Child Relationships

Refresh

April 2019

Views

1.1k time

1

I'm running a test to understand parent/child relationships in Qt and I have a question about how I can view these relationships in the Qt Creator Debugger.

When I start my demo application, here is what the debugger shows:

Application Starts

Because I call Qt's dumpObjectTree() before I add any widgets, the tree is empty, except for the MainWindow's layout. That's what I expected.

When I close the application, and the ~MainWindow destructor is called, I call dumpObjectTree() again, but this time, all of the widgets that I created show in the tree. If I called dumpObjectTree() after the window was destroyed, shouldn't the tree be empty again?

enter image description here

Am I not destroying the child widgets correctly, or do I misunderstand the information displayed by the dumpObjecTree() function?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{

    qDebug() << "WINDOW INITIALIZED-------------";
    dumpObjectTree();
    this->buildLayout();

}

void MainWindow::buildLayout() {

    QWidget *window = new QWidget(this);
    this->setObjectName("Main Window");

    layout = new QVBoxLayout();

    QSplitter *split = new QSplitter();
    split->setObjectName("Horizontal Split");
    split->setOrientation(Qt::Horizontal);
    QTextEdit *editor1 = new QTextEdit();
    editor1->setObjectName("Editor 1");
    QTextEdit *editor2 = new QTextEdit();
    editor2->setObjectName("Editor 2");
    split->addWidget(editor1);
    split->addWidget(editor2);

    QSplitter *split2 = new QSplitter();
    split2->setObjectName("Vertical Split");
    split2->setOrientation(Qt::Vertical);
    QTextEdit *editor3 = new QTextEdit();
    editor3->setObjectName("Editor 3");
    split2->addWidget(split);
    split2->addWidget(editor3);

    QToolBar *mainToolbar = new QToolBar();
    mainToolbar->setObjectName("Main Toolbar");
    mainToolbar->addAction("Main Button 1");
    mainToolbar->addSeparator();
    mainToolbar->addAction("Main Button 2");
    mainToolbar->setMovable(true);

    layout->addWidget(mainToolbar);
    layout->addWidget(split2);

    QToolBar *toolbar = new QToolBar(this);
    toolbar->setObjectName("Mini Toolbar");
    toolbar->addAction("Button 1");
    toolbar->addSeparator();
    toolbar->addAction("Button 2");
    toolbar->setMovable(true);

    QMenuBar *menu = new QMenuBar(this);
    menu->setObjectName("Menu Bar");
    menu->addAction("Menu 1");
    menu->addAction("Menu 2");
    menu->addAction("Menu 3");

    window->setLayout(layout);

    MainWindow::addToolBar(toolbar);
    MainWindow::setMenuBar(menu);
    MainWindow::setCentralWidget(window);

}

MainWindow::~MainWindow()
{

    delete layout;
    qDebug() << "DESTROYED " << this->metaObject()->className();
    qDebug() << "OBJECT TREE-------------";
    dumpObjectTree();
    qDebug() << "OBJECT INFO-------------";
    dumpObjectInfo();

}

Thanks

1 answers

1

Your confusion is that when your MainWindow destructor is called, the window is only part of the way through being destroyed.

In particular, the child widgets do not get deleted through the parent-child mechanism until the QObject destructor is called, which occurs after that, so at the point you call dumpObjectTree() all the children still exist.