问题
As a very simple example, I want to show a dialog in Qt when I press a button. The usual pattern for this (in the application I'm currently working on) seems to be as follows:
class MainWindow {
...
private slots:
buttonClicked();
...
private:
ChildWindow * childWindow;
}
MainWindow::MainWindow(QWidget * parent) : QWidget(parent) {
...
childWindow = new ChildWindow(this);
...
}
MainWindow::buttonClicked() {
childWindow.show();
}
Coming from .NET and Windows Forms (and because I don't need access to that object from elsewhere in the class) the following pattern is more familiar to me:
button1_Clicked(object sender, EventArgs e) {
ChildWindow f = new ChildWindow();
f.Show();
}
The local variable means I don't have yet another instance field and also that the window won't linger around in memory for much longer than necessary. A direct translation of that to C++ would be a bit ugly because no one would clear up afterwards. I tried the following things:
shared_ptr
. No luck, the window isdelete
d as soon as the method ends which means the new window appears for a split second and vanishes again. Not so good.exec()
instead ofshow()
. This would work for modal dialogs, but documentation seemed to imply that it also stops the event loop and that you should callQApplication::processEvents()
regularly if it still needs to be updated. I understand little enough here but I guess it's not too nice either.deleteLater()
. Sadly, just showing a window doesn't blockdeleteLater
so it vanishes as soon as it appears.
Is there a nice option to just clean up after the window when I close it?
回答1:
childWindow->setAttribute( Qt::WA_DeleteOnClose );
Also note that calling exec()
will block execution of the calling event loop, but will spawn its own event loop, so no calls to processEvents()
should be necessary.
回答2:
You can connect the finished()
signal of th dialog to its deleteLater
slot:
ChildWindow * d = new ChildWindow(this);
connect(d, SIGNAL(finished(int)), d, SLOT(deleteLater()));
d->show();
This way it will be delete
d as soon as you close the dialog.
来源:https://stackoverflow.com/questions/15814626/how-to-show-a-window-in-qt-and-deleting-it-as-soon-as-its-closed