There is a multi-window application. Widgets sometimes change parents and move from one window to another. There is a need to close the window with the remnants of the widgets. For example, there were two windows, each with two QLabel . From one window to another passed QLabel . The window with one QLabel remaining in it is no longer needed and can be completely removed with all the widgets in it. How to properly remove this window so that there are no memory leaks and other unpleasant things? Or is the memory freed automatically?

    2 answers 2

    For each window, set the attribute:

     setAttribute(Qt::WA_DeleteOnClose); 

    This attribute does the following:

    Makes Qt delete this widget when the widget has reached the close event (see QWidget :: closeEvent ()).

    In other words, when a window receives a QCloseEvent , it self-destructs.

    • For a window - do you mean for the most important widget in a window whose parent is nullptr ? - LNK
    • @LNK, yes, their parents are responsible for the rest of the widgets - yrHeTaTeJlb
    • And the memory will be released, everything will be removed if I call close(); ? Just now I tried to close the window, and then turn to its descendants (call the show() method and resize() ), which should have been deleted (to check whether the descendants were deleted). Nothing is displayed on the screen, but the process remains running, although there is not a single visible window. Hence the doubts. - LNK
    • @LNK, when a parent is destroyed, it destroys all descendants. The top-level widget (window) does not have a parent, but thanks to the Qt::WA_DeleteOnClose it will self-destruct on closing. If in doubt, you can take this class , write new Watcher(this); at the end of the constructor of any window new Watcher(this); and make sure everything works - yrHeTaTeJlb
    • one
      @LNK, if you rephrase your question, you get "What will happen if you turn to a remote object?". Answer - Undefined Behavior. The program can continue to work normally, it can crash with an error, it can fill all the available memory with zeros, it can format the hard disk. In short, when accessing remote objects, anything can happen, and it will be within the standard. - yrHeTaTeJlb

    The parent widget will automatically remove its children when destroyed.

    If you’re looking for something, you’ll be able to create it. If the object has been taken away, it would be possible to remove the object from its parent. If the object has children, the destructor automatically deletes each child. No QObject is deleted twice, regardless of the order of destruction.

    Object Trees & Ownership

    In other words, if you have something displayed in the window, it is enough to remove the window widget, the system of object hierarchies Qt will take care of the rest.

    • That is, if I just close the window through the close() slot, everything will be removed correctly and so? - LNK
    • 2
      @LNK Must be called by a destructor. This will happen if you follow the advice of a supercharger. - free_ze