I have two function, one is called at the start time, and the other is called whenever i want to create a new database.

Qt Code:
  1. bool mainWindow::createConnection()
  2. {
  3. db = QSqlDatabase::addDatabase("QSQLITE");
  4. /* If the database do not exist
  5. I create one anew*/
  6. QFile check;
  7. check.setFileName(databaseName);
  8. QDir::setCurrent(QCoreApplication::applicationDirPath());
  9. #ifdef DEBUG
  10. qDebug()<<QCoreApplication::applicationDirPath();
  11. #endif
  12. db.setDatabaseName(databaseName);
  13. if (!check.exists()){
  14. QMessageBox::warning(0,QObject::tr("Database mancante"),"Sembra che il database precedente sia stato cancellato. Ne ho creato uno nuovo con lo stesso nome");
  15. #ifdef DEBUG
  16. qDebug()<<databaseName<<db.open();
  17. #endif
  18. createTable();
  19. }
  20. if (!db.open()) {
  21. QMessageBox::warning(0,QObject::tr("Non riesco a connettermi al database"),"Controlla che un databse sia presente o scegline uno dal menu File");
  22. #ifdef DEBUG
  23. qDebug()<<db.lastError()<<db.open();
  24. #endif
  25. return false;
  26. }
  27. return true;
  28.  
  29. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. void mainWindow::newDB()
  2. {
  3. /* Creat un nuovo archivio*/
  4. if (db.isOpen())
  5. {
  6. removeConnection();
  7. #ifdef DEBUG
  8. qDebug()<<"newDB, closing conn.:"<<db.isOpen();
  9. #endif
  10. }
  11. QString fileName = QFileDialog::getSaveFileName(this, tr("Nuovo archivio"),QDir::homePath (),tr("Rubic (*.db)"));
  12. db = QSqlDatabase::addDatabase("QSQLITE");
  13. db.setDatabaseName(fileName);
  14. #ifdef DEBUG
  15. qDebug()<<"newDB, creating"<<db.lastError()<<db.isOpen()<<fileName<<db.isValid();
  16. #endif
  17. if (db.isValid()){
  18. createTable();
  19. databaseName=fileName;
  20.  
  21. }
  22.  
  23.  
  24. }
To copy to clipboard, switch view to plain text mode 

But the debug says to me that, when I call newDB(), removeConnection() and db = QSqlDatabase::addDatabase("QSQLITE");, there is still in use the qt_sql_default connection.
The function removeConnection() is
Qt Code:
  1. void mainWindow::removeConnection()
  2. {
  3. // db.close();
  4.  
  5. QSqlDatabase::removeDatabase(db.connectionName());
  6. }
To copy to clipboard, switch view to plain text mode 

For this reason, I cannot start a new database.
Help appreciated.