Important: Please read the Qt Code of Conduct -

How to destroy second window when closed

  • hello,

    I'm working on a new qt project, and I need to be sure to manage correctly the memory allocation and the resources. Just want to ask if someone can give me some suggestion about the way to correctly close a second window, opened from the main window, in particular I need to de-allocate the memory when window is closed.

    Thanks for the support

  • Moderators

    there are multiple ways to achieve this.

    1. you can set the parent of the secondary window to the mainwindow. And on the main window set the Qt::WA_DeleteOnClose widget attribute. Then the secondary window is closed automatically when the mainwindow is closed.
    2. do it manually at any time by keeping a pointer to the secondary window and call close() on it
    3. ...

  • For modal dialogs you normally just allocate on the stack, and call the blocking exec (with a bit of caution)

    for any other sub window you can use setAttribute(Qt::WA_DeleteOnClose);

  • thank you a lot for the fast reply! It works now.

Log in to reply