3

Let's say I have 2 windows in my application, and two classes responsible for them: class MainWindow: public QMainWindow and class SomeDialog: public QWidget.

In my Main Window I have a button. When it is clicked, I need to display the second window. I do it this way:

SomeDialog * dlg = new SomeDialog();
dlg.show();

Now, the user does something in the window, and closes it. At this point I want to get some data from that window, and then, I suppose, I will have to delete dlg. But how do I catch the event of that window being closed?

Or is there another way not to have a memory leak? Maybe It would be better to create an instance of each window on startup, and then just Show()/Hide() them?

How do I manage such a case?

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105

3 Answers3

3

It is advised to use show() / exec() and hide() instead of dynamically creating the dialog every time you want to show it. Also use QDialog instead of QWidget.

In the constructor of your main window create it and hide it

MainWindow::MainWindow()
{
     // myDialog is class member. No need to delete it in the destructor
     // since Qt will handle its deletion when its parent (MainWindow)
     // gets destroyed. 
     myDialog = new SomeDialog(this);
     myDialog->hide();
     // connect the accepted signal with a slot that will update values in main window
     // when the user presses the Ok button of the dialog
     connect (myDialog, SIGNAL(accepted()), this, SLOT(myDialogAccepted()));

     // remaining constructor code
}

In the slot connected to the buttons's clicked() event simply show it, and if necessary pass some data to the dialog

void myClickedSlot()
{
    myDialog->setData(data);
    myDialog->show();
}

void myDialogAccepted()
{
    // Get values from the dialog when it closes
}
pnezis
  • 12,023
  • 2
  • 38
  • 38
2

Subclass from QWidget and reimplement

virtual void QWidget::closeEvent ( QCloseEvent * event )

http://doc.qt.io/qt-4.8/qwidget.html#closeEvent

Also it looks like the widget you want to show is a dialog. So consider using QDialog or it's subclasses. QDialog has useful signals you can connect to:

void    accepted ()
void    finished ( int result )
void    rejected ()
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Andrew
  • 24,218
  • 13
  • 61
  • 90
1

I think you are looking for the Qt::WA_DeleteOnClose window flag: http://doc.qt.io/archives/qt-4.7/qt.html#WidgetAttribute-enum

QDialog *dialog = new QDialog(parent);
dialog->setAttribute(Qt::WA_DeleteOnClose)
// set content, do whatever...
dialog->open();
// safely forget about it, it will be destroyed either when parent is gone or when the user closes it.
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
MrFox
  • 1,244
  • 1
  • 11
  • 24
  • The ctor takes window flags, not widget attributes. The correct call is dialog->setAttribute( Qt::WA_DeleteOnClose ) – Frank Osterfeld Jul 28 '12 at 08:02
  • @FrankOsterfeld thanks. I couldn't compile when I typed that up so I never checked. – MrFox Jul 28 '12 at 16:49
  • @FrankOsterfeld Closing the dialog means? Say I have a button and call the slot close(), what happens then? Will the dialog be cleared from memory? – jxgn Oct 09 '15 at 10:58
  • and what happens if I use deletelater() instead of close()? – jxgn Oct 09 '15 at 11:05