[Qt-interest] application won't terminate

Ferenc Stelcz ferenc at stelcz.hu
Sun Jan 3 21:38:02 CET 2010


On 2010.01.03. 7:49 du., Kurtis Nusbaum wrote:
> My understanding of Qt says the following code should work. I have a
> MainWindow that upon startup prompts the user for a password. The password
> is then hashed and compared to another hash value. If the two do not match
> the application should quit. But for some reason, when I call the close()
> function, the Main window will close but the application does not terminate.
> Anybody have some ideas? When I call the close function in other places in
> my code the program terminates just fine.
>
> Main function:
> int main(int argc, char *argv[])
> {
>      QApplication a(argc, argv);
>      MainWindow w;
>      w.show();
>      w.checkPassword();
>      return a.exec();
> }
>
>
> MaindWindow CheckPassword Function:
> void MainWindow::checkPassword(){
>          if(passwordHash.isEmpty()){
>                  setupNewPassword();
>          }
>          else{
>                  bool ok = true;
>                  QString passwordInput = QInputDialog::getText(this,
> tr("Enter Password"), tr("Password:"), QLineEdit::Password, QString(),&ok);
>                  if(ok){
>                          QByteArray inputHash =
> QCryptographicHash::hash(passwordInput.toUtf8(), QCryptographicHash::Sha1);
>                          if(inputHash != passwordHash){
>                                  QMessageBox::critical(this, "Error",
> "Incorrect Password");
>                                  close();
>                          }
>                  }
>                  else{
>                          QMessageBox::critical(this, "Error", "Bad
> Password");
>                          close();
>                  }
>          }
> }
>
> MainWindow closeEvent function:
> void MainWindow::closeEvent(QCloseEvent *event){
>          saveSettings();
>          event->accept();
> }
>
> -Kurtis

Hello!

It's been a long time since I was playing around with stuff like this, but the 
close() method is the one of the QMainWindow instance you are displaying as 
your main window. If you want to exit the whole application you should replace 
close() with qApp->exit(int retval) or qApp->quit()  (Where qApp is: "A global 
pointer referring to the unique application object.") based on whether you 
want to exit with a `custom` return value (e.g. -1)[exit(int)] or simply 0 
[quit()].

HTH

--
Ferenc Stelcz
Ventyx Asset Suite 6 systems administrator



More information about the Qt-interest-old mailing list