Hi everyone,
I am trying mysql database connection with QSqlDatabase and it's behaving really strange. When I have only one button in the GUI, everything works fine. But when I add Line Edit, which doesn't do anything, program hangs for about 4 seconds on exit.
Only thing the program does is that it creates connection to database in constructor and then closes it in destructor.

Here is the code:
Qt Code:
  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6. db = QSqlDatabase::addDatabase("QMYSQL", "connection");
  7. db.setHostName("127.0.0.1");
  8. db.setDatabaseName("db");
  9. db.setUserName("user");
  10. db.setPassword("password");
  11. connected = db.open();
  12. }
  13.  
  14. MainWindow::~MainWindow()
  15. {
  16. db.close();
  17. db = QSqlDatabase();
  18. QSqlDatabase::removeDatabase("connection");
  19. delete ui;
  20. }
To copy to clipboard, switch view to plain text mode 

If I put this code in "on_pushButton_clicked()" function, program exits correctly. But is it okay to create and remove connection to database in every function of the program, when I want to create more complex database solution?

Another strange thing is that I had to add "db = QSqlDatabase();" before I call removeDatabase, because it showed warning about connection being used when program exited.

Is someone here who can explain it to me, please?