Is it profitable to limit the volume of Qt objects?

Qt objects highlighted with help neware pretty much handled for you. Things will be cleared at some point (almost always when the parent is destroyed), because Qt objects have a good parent relationship with the children.

So my question is this: given that some widgets exist for the life of the application, is it considered good / useful to limit the scope of some child widgets? It seems to me that if I donโ€™t attach, the application will not be able to free these objects until the application exits. For instance:

MyMainWindow::contextMenu(...) {
    QMenu *menu = new QMenu(this);
    // ...
    menu->exec();
}

vs

MyMainWindow::contextMenu(...) {
    QMenu *menu = new QMenu(this);
    // ...
    menu->exec();
    delete menu;
}

vs

MyMainWindow::contextMenu(...) {
    QScopedPointer<QMenu> menu(new QMenu(this));
    // ...
    menu->exec();
}

, , , , , . . , Qt? Qt ?

+3
2

, ( MyMainWindow)... , , , , , contextMenu() , QMenu , / MyMainWindow .

. , , , , - , .

+3

, . , , :

MyMainWindow::showMyDialog() {
    QDialog *d = new MyDialog(); // Oftentimes parents don't make sense for dialogs
    d->setAttribute( Qt::WA_DeleteOnClose );
    // ... presumably connect signals/slots here
    d->show();
} // d isn't deleted yet, but will be once the dialog is accepted or rejected 
  // (and all corresponding signals are emitted).
+1

Source: https://habr.com/ru/post/1726816/


All Articles