November 15, 2011

Hostel Hostel
Hobby Entomologist
185 posts

[Solved]Login dialog and problem with quit app

 

I have a problem with closing properly an application. 8 months ago I found a web with solution, but now that url is broken and I need yours help. I remember that problem is in application event loop, because when dialog is analised and closed then an event loop is not running yet. Here is my code – what should I add to resolve this issue?

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.  
  5.     LogInDialog logInDialog; // QDialog class
  6.  
  7.     logInDialog.exec();
  8.     int dialogResult = logInDialog.result();
  9.     if( dialogResult == QDialog::Accepted )
  10.     {
  11.         MainWindow w;
  12.         w.show();
  13.     }
  14.     // if a dialogResult is not equal QDialog::Accepted then I want to close app, but now an application is running
  15.     return a.exec();
  16. }

5 replies

November 15, 2011

BilbonSacquet BilbonSacquet
Lab Rat
75 posts

I don’t know exactly what is your challenge here, but you don’t need to start the application message loop:

  1. if (dialogResult != QDialog::Accepted)
  2.     return -1;
  3.  
  4. MainWindow w;
  5. w.show();
  6.    
  7. return a.exec();

November 16, 2011

rokemoon rokemoon
Lab Rat
197 posts

Also you can write like this:

  1. LogInDialog logInDialog; // QDialog class
  2.  
  3. if (logInDialog.exec() != QDialog::Accepted)
  4.     return -1;
  5.  
  6. MainWindow w;
  7. w.show();
  8.      
  9. return a.exec();

So as I understand you have two buttons in your dialog (Ok & Cancel),
if you want that dialog return you QDialog::Accepted or QDialog::Rejected
you need to connect your buttons to dialog’s slots accept [doc.qt.nokia.com] and reject [doc.qt.nokia.com]
Example:
  1. class TestDialog: public QDialog
  2. {
  3. public:
  4.  TestDialog()
  5.  {
  6.                                       | QDialogButtonBox::Cancel);
  7.  
  8.      connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  9.      connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  10.      QVBoxLayout *layout = new QVBoxLayout();
  11.      layout->addWidget(buttonBox);
  12.      setLayout(layout);
  13.  }
  14. };

and main
  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.  
  5.     TestDialog dlg;
  6.     if (dlg.exec() != QDialog::Accepted)
  7.       return -1;
  8.     Widget w;
  9.     w.show();
  10.  
  11.     return a.exec();
  12. }

All works fine.

November 16, 2011

Hostel Hostel
Hobby Entomologist
185 posts

Thanks for reply. Unnecessery I was looking for complicated solution – in many cases, the simple solution is the best solution.

November 16, 2011

rokemoon rokemoon
Lab Rat
197 posts

Hostel wrote:
in many cases, the simple solution is the best solution.

Yep you are right

November 16, 2011

Hostel Hostel
Hobby Entomologist
185 posts

I tested a solution and I have a conclusion:

This is wrong:

  1. return 0;

In Qt Creator I have a message that application was unexpected closed.

This is good:

  1. exit( 0 );

 
  ‹‹ Need save/restore QMainWindow minimized state      Socket gets called twice!? ››

You must log in to post a reply. Not a member yet? Register here!