[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?
@
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();
}
@ -
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();
@ -
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.