Hi all, I just started qt, so I don't have enough knowledge.

Qt Code:
  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6.  
  7. connectDatabase();
  8. }
  9.  
  10. void MainWindow::connectDatabase()
  11. {
  12. QFile databaseFile("database.db");
  13. if (!databaseFile.exists()) {
  14. connectionError();
  15. } else
  16. {
  17. db.setHostName("localhost");
  18. db.setDatabaseName("database.db");
  19. if (!db.open()) {
  20. connectionError();
  21. }
  22. }
  23. }
  24.  
  25. void MainWindow::connectionError()
  26. {
  27. QMessageBox msgBox;
  28. msgBox.addButton(QMessageBox::Yes);
  29. msgBox.addButton(QMessageBox::No);
  30. msgBox.setWindowTitle("Connection Error");
  31. msgBox.setIcon(QMessageBox::Critical);
  32. msgBox.setText("Program cannot connect the database file which is required to run this software. Would you like to download default database file? Please note that all of the saved process data will be removed.");
  33. int selection = msgBox.exec();
  34.  
  35. if (selection == QMessageBox::Yes) {
  36. //restore data
  37. } else {
  38. MainWindow::close();
  39. }
  40.  
  41. }
To copy to clipboard, switch view to plain text mode 


This is what I did. When I delete the database file to see connectionError function, messagebox comes before the MainWindow and MainWindow::close() doesnt work. What should I do?