[Solved]Login dialog and problem with quit app
-
wrote on 15 Nov 2011, 19:02 last edited by
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?
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);LogInDialog logInDialog; // QDialog class logInDialog.exec(); int dialogResult = logInDialog.result(); if( dialogResult == QDialog::Accepted ) { MainWindow w; w.show(); } // if a dialogResult is not equal QDialog::Accepted then I want to close app, but now an application is running return a.exec();
}
@ -
wrote on 15 Nov 2011, 20:59 last edited by
I don't know exactly what is your challenge here, but you don't need to start the application message loop:
@
if (dialogResult != QDialog::Accepted)
return -1;MainWindow w;
w.show();return a.exec();
@ -
wrote on 16 Nov 2011, 06:10 last edited by
Also you can write like this:
@
LogInDialog logInDialog; // QDialog classif (logInDialog.exec() != QDialog::Accepted)
return -1;MainWindow w;
w.show();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":http://doc.qt.nokia.com/latest/qdialog.html#accept and "reject":http://doc.qt.nokia.com/latest/qdialog.html#reject
Example:
@
class TestDialog: public QDialog
{
public:
TestDialog()
{
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(buttonBox); setLayout(layout);
}
};
@
and main
@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);TestDialog dlg; if (dlg.exec() != QDialog::Accepted) return -1; Widget w; w.show(); return a.exec();
}
@
All works fine. -
wrote on 16 Nov 2011, 08:45 last edited by
Thanks for reply. Unnecessery I was looking for complicated solution - in many cases, the simple solution is the best solution.
-
wrote on 16 Nov 2011, 09:17 last edited by
[quote author="Hostel" date="1321433146"]in many cases, the simple solution is the best solution.[/quote]
Yep you are right -
wrote on 16 Nov 2011, 09:38 last edited by
I tested a solution and I have a conclusion:
This is wrong:
@
return 0;
@
In Qt Creator I have a message that application was unexpected closed.This is good:
@
exit( 0 );
@
5/6