PDA

View Full Version : Having a QSqlDatabase as a class member and avoid the connection still in use warning



qlands
16th October 2014, 09:48
Hi,

I have a class that has a QSqlDatabase as a private member.


private:
QSqlDatabase m_database;

I use m_database in several functions of the class like:



QSqlQuery *query = new QSqlQuery(m_database);

..

delete query;


However in the destructor of the class I remove the connection with


m_database.close();
QSqlDatabase::removeDatabase("MyConnection");

But I always get the warning "MyConnection' is still in use, all queries will cease to work."

How can I avoid the warning?

wysota
16th October 2014, 10:14
Set the database object to an empty one before removing the database:


m_database.close();
const QString cname = m_database.connectionName();
m_database = QSqlDatabase();
QSqlDatabase::removeDatabase(cname);