PDA

View Full Version : new instance of dialog



gemidjy
28th February 2008, 17:42
Hello,

I have this QMainWindow form and this QDialog form that is sort of child of QMainWindow one. So when a button is cliecked in the mainWidget I want the dialog to appear. I do it like:

as global variable define dialog's instance:



dialog *dialogWindow;


then I initialize the instance and show it:


void mainWindow::showDialog()
{
dialogWindow=new dialog(this);
dialog->show();
}


And it shows. But if I make changes in the dialog's interface (say, put a value in a lineedit that appears in the dialog), and close the dialog, and then call/show it again from the mainWindow, I don't get new dialog instance, but the very same one, with the last values I've put in it. I tried to do setAttribute(Qt::WA_DeleteOnClose); but it doesn't work.

Any idea?

THRESHE
28th February 2008, 17:58
Maybe you should delete this dialog and create it again to obtain blank dialog ? :)

If dialog is modal I suggest to use exec and delete dialog after execution (or just create it on stack)

pherthyl
28th February 2008, 18:11
You're leaking memory by creating a new dialog every time. Either you need to delete it first


if(dialogWindow) delete dialogWindow;

(of course you have to initialize dialogWindow to 0 in the constructor. Or you create it on the stack and use exec() to get a modal dialog as THRESHE mentioned.

The third option is to have a clear() function or equivalent in your dialog that resets its internal state so you dont have to delete and recreate it (will be faster).

gemidjy
28th February 2008, 18:27
You're leaking memory by creating a new dialog every time. Either you need to delete it first


if(dialogWindow) delete dialogWindow;

(of course you have to initialize dialogWindow to 0 in the constructor. Or you create it on the stack and use exec() to get a modal dialog as THRESHE mentioned.

The third option is to have a clear() function or equivalent in your dialog that resets its internal state so you dont have to delete and recreate it (will be faster).

Great, thanks both of ya. Will implement the 'clear()'-way you suggested! :)