title: QSqlDatabase: How to avoid 'qt_sql_default_connection' still in use & duplicate connection related warning?
body: <p>Sorry if this is a trivial question but I have been trying to build a small .ui that used <code>QSQLITE</code> as database and that uses <code>QTableView</code> to show 4 columns on a default database file as example. </p>
  
  <p>I debugged the problem in every side, changed logical operations of the <code>SQL</code> and restructured the constructor in a more simple way but the error still stays.</p>
  
  <p>After finishing to set up all parameters I am getting this error:</p>
  
  <p><code>QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.</code></p>
  
  <p>And </p>
  
  <p><code>QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.</code></p>
  
  <p>I looked on several sources that describe this error such as <a href="https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase" rel="nofollow noreferrer">this source</a>, this <a href="https://www.qtcentre.org/threads/47303-Qt-Check-if-there-is-an-open-database-connection" rel="nofollow noreferrer">other source</a>. Also <a href="https://stackoverflow.com/questions/48827940/qsqldatabaseprivateremovedatabase-connection-myconnectionname-is-still-in-u">this</a> was useful but still nothing happens. Official documentation suggests a "wrong" and "right" way to do that <a href="https://doc.qt.io/qt-5/qsqldatabase.html#removeDatabase" rel="nofollow noreferrer">here</a>. but the error still stays. After all these different options I brought back the code to a more concise way and I hope that someone can shed light on this issue.</p>
  
  <p>Below the snipped of code: </p>
  
  <p><strong>mainwindow.h</strong></p>
  
  <pre><code>private:
      QString temporaryFolder;
      dataInfo *mNewDatabaseImages;
      QSqlTableModel *mNewTableImages;
  </code></pre>
  
  <p><strong>mainwindow.cpp</strong></p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      temporaryFolder = "/home/to/Desktop/tempDBFolder/tmp.db";
      QFile dbRem(temporaryFolder);
      dbRem.remove();
      mNewDatabaseImages = new dataInfo(this);
      mNewDatabaseImages-&gt;initDataBase(temporaryFolder);
      mNewDatabaseImages-&gt;confDataBase();
      mNewTableImages = new QSqlTableModel(this, mNewDatabaseImages-&gt;getDatabase());
      mNewTableImages-&gt;setTable("leftCamTable");
      mNewTableImages-&gt;select();
      ui-&gt;bookMarkTableView-&gt;setModel(mNewTableImages);
      ui-&gt;bookMarkTableView-&gt;showColumn(true);
  }
  </code></pre>
  
  <p><strong>datainfo.h</strong></p>
  
  <pre><code>#ifndef DATAINFO_H
  #define DATAINFO_H
  #include &lt;QObject&gt;
  #include &lt;QSqlDatabase&gt;
  #include "imageparam.h"
  
  class dataInfo : public QObject
  {
      Q_OBJECT
  public:
      explicit dataInfo(QObject *parent = nullptr);
      bool initDataBase(const QString &amp;nameDB);
      bool confDataBase();
      bool addItem(ImageParam* imageItem);
      QSqlDatabase getDatabase();
  private:
      QString mError;
      QSqlDatabase mDBImages;
  };
  #endif // DATAINFO_H
  </code></pre>
  
  <p><strong>datainfo.cpp</strong></p>
  
  <pre><code>#include "datainfo.h"
  #include &lt;QSqlQuery&gt;
  #include &lt;QSqlError&gt;
  #include &lt;QDebug&gt;
  #include &lt;QVariant&gt;
  #include &lt;QMessageBox&gt;
  
  #define CREATE_TABLE \
      " CREATE TABLE IF NOT EXISTS imageTable" \
      " (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
      " path1 TEXT NOT NULL" \
      " path2 TEXT NOT NULL" \
      " imageA BLOB NOT NULL" \
      " imageB BLOB NOT NULL)"
  
  dataInfo::dataInfo(QObject *parent) : QObject(parent)
  {}
  
  bool dataInfo::initDataBase(const QString &amp;nameDB)
  {
      mDBImages = QSqlDatabase::addDatabase("QSQLITE");
      mDBImages.setDatabaseName(nameDB);
      bool ok = mDBImages.open();
      if(!ok) {
          mError = mDBImages.lastError().text();
          qDebug() &lt;&lt; mError;
      }
      return ok;
  }
  
  bool dataInfo::confDataBase()
  {
      QSqlQuery qry;
      bool ok = qry.exec(CREATE_TABLE);
      if(!ok) {
          mError = qry.lastError().text();
      }
      return ok;
  }
  
  bool dataInfo::addItem(ImageParam *imageItem)
  {
      QSqlQuery qry;
      qry.prepare("INSERT INTO imageTable (path1, path2, imageA, imageB)" \
                  " VALUES (?,?,?,?)");
      qry.addBindValue(imageItem-&gt;path1());
      qry.addBindValue(imageItem-&gt;path2());
      qry.addBindValue(imageItem-&gt;image1());
      qry.addBindValue(imageItem-&gt;image2());
      bool ok = qry.exec();
      if(!ok) {
          mError = qry.lastError().text();
      }
      return ok;
  }
  
  QSqlDatabase dataInfo::getDatabase()
  {
      return mDBImages;
  }
  </code></pre>
  
  <p>I looked also at <a href="https://forum.qt.io/topic/15962/solved-qsqldatabaseprivate-removedatabase-connection-is-still-in-use-all-queries-will-cease-to-work/3" rel="nofollow noreferrer">this post</a> that suggested to set a name to the databse first but I already do that in the function <code>initDataBase(const QString &amp;nameDB)</code>. Here the <a href="https://forum.qt.io/topic/15962/solved-qsqldatabaseprivate-removedatabase-connection-is-still-in-use-all-queries-will-cease-to-work/3" rel="nofollow noreferrer">post</a> that suggested the procedure, which is below:</p>
  
  <pre><code>db-&gt;setDatabaseName("name");
  if(!db-&gt;open()) {
      qDebug() &lt;&lt; "Error opening ";
      return false;
  }
  </code></pre>
  
  <p>Please shed light on the possible solution.</p>