Should we remove the layout in the destructor?



  • MainWindow.cpp

    MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
    {
        label = new QLabel(this);
        label->setText(tr("Text:"));
    
    line = new QLineEdit(this);
    output = new QPlainTextEdit(this);
    
    layoutHorizontal = new QHBoxLayout();
    layoutHorizontal->addWidget(label);
    layoutHorizontal->addWidget(line);
    
    layoutVertical = new QVBoxLayout();
    layoutVertical->addLayout(layoutHorizontal);
    layoutVertical->addWidget(output);
    
    setLayout(layoutVertical);
    

    }

    How do I understand layoutHorizontal and layoutVertical in the hierarchy of Qt objects, and will not be removed by chain? MainWindow is here only for example.



  • You don't think these objects will be in hierarchy. layoutHorizontal It's like, "through." layoutVertical Here we go.

    layoutVertical->addLayout(layoutHorizontal);
    

    layoutVertical It's going to become a descendant. MainWindow Here we go.

    setLayout(layoutVertical);
    

    Accordingly, it's clear to destroy them in the destructor. MainWindow No need.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2