Results 1 to 4 of 4

Thread: new instance of dialog

  1. #1
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default new instance of dialog

    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:

    Qt Code:
    1. dialog *dialogWindow;
    To copy to clipboard, switch view to plain text mode 

    then I initialize the instance and show it:
    Qt Code:
    1. void mainWindow::showDialog()
    2. {
    3. dialogWindow=new dialog(this);
    4. dialog->show();
    5. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Mar 2007
    Location
    Ukraine, Odessa
    Posts
    140
    Thanks
    15
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: new instance of dialog

    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)
    C++ & AMD forever

  3. The following user says thank you to THRESHE for this useful post:

    gemidjy (28th February 2008)

  4. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: new instance of dialog

    You're leaking memory by creating a new dialog every time. Either you need to delete it first

    Qt Code:
    1. if(dialogWindow) delete dialogWindow;
    To copy to clipboard, switch view to plain text mode 

    (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).

  5. The following user says thank you to pherthyl for this useful post:

    gemidjy (28th February 2008)

  6. #4
    Join Date
    Feb 2008
    Posts
    16
    Thanks
    3

    Default Re: new instance of dialog

    Quote Originally Posted by pherthyl View Post
    You're leaking memory by creating a new dialog every time. Either you need to delete it first

    Qt Code:
    1. if(dialogWindow) delete dialogWindow;
    To copy to clipboard, switch view to plain text mode 

    (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!

Similar Threads

  1. Resizing the dialog boxes
    By anju123 in forum Qt Programming
    Replies: 4
    Last Post: 14th September 2007, 11:41
  2. QGraphicsView: Dialog Position Doubt
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2007, 18:48
  3. Communication between MainWindow and a dialog
    By Backslash in forum Newbie
    Replies: 9
    Last Post: 3rd August 2007, 05:27
  4. Replies: 3
    Last Post: 23rd July 2006, 19:02
  5. Dialog positioning in GNOME
    By simk in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2006, 10:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.