[Qt-interest] use button to launch the main window

Rob Douglas rwdougla at gmail.com
Mon Dec 1 15:32:52 CET 2008


S. Aguinaga wrote:
> Thank you, thank you Rob!
> That worked just fine.  Regarding the last comment, I don't understand, 
> the new window is in the stack, and if I some how quit this function 
> (such as click on the border X button) it will destroy it?
> 
> What would be the elegant way end this new window and clean up any stack 
> or memory related stuff?

[snip]

The new window:

QMainWindow* meawin = new QMainWindow(this);

will be on the heap. This means that its lifetime goes until someone 
calls "delete" on the value of the pointer meawin. Contrast this with 
the first version:

QMainWindow meawin(this);

whose lifetime lasts only with the scope it is in. In this case, its 
scope is the function. Once the function returns, it is cleaned up, and 
the window goes away if it is visible.

If you want to ensure that the window is deleted when closed, you can 
manually set "Qt::WA_DeleteOnClose" with
void QWidget::setAttribute ( Qt::WidgetAttribute attribute, bool on = true )
for the window. I am not positive, and perhaps someone else could 
clarify this, but I believe that this is on by default.

Hope this helps,
-Rob



More information about the Qt-interest-old mailing list