Results 1 to 7 of 7

Thread: How to close (exit) a dialog properly

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to close (exit) a dialog properly

    Hi everyone,

    I need open a new dialog (form) from main form.
    Qt Code:
    1. void MainScr::on_pushButton_2_clicked()
    2. {
    3. if (!cyclescrdialog)
    4. cyclescrdialog = new cycleScrDialog(NULL);
    5. cyclescrdialog->exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    in the beginning of the new form, i initialize two leds, "label_led_1" and "label_led_2", to be green.
    Qt Code:
    1. cycleScrDialog::cycleScrDialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::cycleScrDialog)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QPoint pos; // relocate window
    8. pos.setX(0); // x
    9. pos.setY(0); // y
    10. move(pos);
    11.  
    12. picLedRed = QPixmap("/home/Pictures/ledred.png");
    13. picLedGreen = QPixmap("/home/Pictures/ledgreen.png");
    14.  
    15. ui->label_led_1->setPixmap(picLedGreen);
    16. ui->label_led_2->setPixmap(picLedGreen);
    17. // ....
    18. // ....
    19. }
    20.  
    21. cycleScrDialog::~cycleScrDialog()
    22. {
    23. delete ui;
    24. }
    25.  
    26. // exit button
    27. void cycleScrDialog::on_pushButton_4_clicked()
    28. {
    29. close();
    30. }
    To copy to clipboard, switch view to plain text mode 

    in the middle of program, these leds will be changed to be red.
    after i click "exit" button to close this window (dialog), and re-open it, I found that these two leds are still red.
    When i step through my program, it did not go through the beginning of cycleScrDialog. it means that the close() function does not close completely, all variables in cycleScrDialog has not been released from memory.

    can anyone point me where i did wrong, and how to close a dialog properly?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to close (exit) a dialog properly

    it means that the close() function does not close completely, all variables in cycleScrDialog has not been released from memory.
    You create you Dialog on heap

    Qt Code:
    1. if (!cyclescrdialog)
    2. cyclescrdialog = new cycleScrDialog(NULL);
    To copy to clipboard, switch view to plain text mode 

    so you don't delete it from memory.

    Try with
    Qt Code:
    1. cycleScrDialog cyclescrdialog (NULL);
    To copy to clipboard, switch view to plain text mode 

    In this way you create and delete it each time
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to close (exit) a dialog properly

    Hi mcosta, thanks for your reply.
    I have tried your code, but the program stop and show an error message:
    The program has unexpectedly finished.
    i am a newbie to Qt, when i learned how to create a new dialog (window), i studied some codes from this forum. Here is how i understand to create a new dialog, and i create all my new dialogs in the same way. can you point me is there anything wrong in my code, please?

    in main frame (mainscr), i have a push button to open a second dialog (cyclescrdialog).
    in mainscr.cpp
    Qt Code:
    1. #include "mainscr.h"
    2. #include "ui_mainscr.h"
    3. // ******
    4. #include "cyclescrdialog.h"
    5. // ******
    6. #include "qextserialport.h"
    7. #include <qmessagebox.h>
    8.  
    9. extern cycleScrDialog *cyclescrdialog; //*****
    10.  
    11. MainScr::MainScr(QWidget *parent) :
    12. QMainWindow(parent),
    13. ui(new Ui::MainScr)
    14. {
    15. ui->setupUi(this);
    16. //...
    17. //...
    18. }
    19.  
    20. void MainScr::on_pushButton_1_clicked()
    21. {
    22. if (!cyclescrdialog)
    23. //cyclescrdialog = new cycleScrDialog(NULL);
    24. cycleScrDialog cyclescrdialog(NULL); //if i try this code, program will stop
    25. cyclescrdialog->exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    in cyclescrdialog.cpp
    Qt Code:
    1. #include "cyclescrdialog.h"
    2. #include "ui_cyclescrdialog.h"
    3. #include <qmessagebox.h>
    4.  
    5. cycleScrDialog *cyclescrdialog;
    6.  
    7. cycleScrDialog::cycleScrDialog(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::cycleScrDialog)
    10. {
    11. ui->setupUi(this);
    12.  
    13. // remove window frame
    14. Qt::WindowFlags flags = windowFlags();
    15. if (flags.testFlag(Qt::FramelessWindowHint))
    16. return;
    17. flags |= Qt::FramelessWindowHint;
    18. setWindowFlags(flags);
    19. //...
    20. //...
    21. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to close (exit) a dialog properly

    Correct your cpp file as

    Qt Code:
    1. #include "mainscr.h"
    2. #include "ui_mainscr.h"
    3. // ******
    4. #include "cyclescrdialog.h"
    5. // ******
    6. #include "qextserialport.h"
    7. #include <qmessagebox.h>
    8.  
    9. MainScr::MainScr(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainScr)
    12. {
    13. ui->setupUi(this);
    14. //...
    15. //...
    16. }
    17.  
    18. void MainScr::on_pushButton_1_clicked()
    19. {
    20. cycleScrDialog cyclescrdialog(NULL);
    21. cyclescrdialog.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    When on_pushButton_1_clicked exits cyclescrdialog is destroyed
    A camel can go 14 days without drink,
    I can't!!!

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

    cooper (9th March 2011)

  6. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to close (exit) a dialog properly

    Use

    cyclescrdialog.show();

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to close (exit) a dialog properly

    Use

    cyclescrdialog.show();
    Please don't post wrong answers - show() does not block, so you'll only see a quick flash of your dialog, as it'll be destroyed immediately after calling show() ( when exiting the method ). Using exec() blocks until dialog is closed by the user.

  8. The following user says thank you to stampede for this useful post:

    cooper (9th March 2011)

  9. #7
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to close (exit) a dialog properly

    It works as what i need. Thanks for all you guys help. i really appreciate that

Similar Threads

  1. Properly close database handlers
    By ruben.rodrigues in forum Newbie
    Replies: 5
    Last Post: 23rd September 2010, 16:10
  2. program exit itself when create dialog with QSystemTrayIcon
    By jim2212001 in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2010, 11:35
  3. How do i close a buttonless dialog.
    By geekshlby in forum Newbie
    Replies: 12
    Last Post: 12th January 2010, 08:26
  4. Non-modal dialog forces app to exit.
    By vereteran in forum Newbie
    Replies: 1
    Last Post: 17th October 2009, 08:39
  5. Close Dialog in showEvent
    By pospiech in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 15:32

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.