QSqlDatabase database seems not to stay open
-
Hi,
i have a QT application with QSqlDatabase . This is what i do :
in cmasterlist.h i have a class with m_db member
class CMasterList : public QObject { private: QSqlDatabase m_db; public: void memberfunction (); }in cmasterlist.c i have a memberfunction
void CMasterList::memberfunction () { if(!m_db.isOpen())return; ... }in main.h i have a class where i create a CMasterList member. Main.h and Main.c is the main class for my ui application which means the destructor is only called when i close the application.
class Cmain : public QMainWindow { private: CMasterList m_MasterList; }in the construtor of Cmain i first open the db m_db .
the problem that i have :
when i call m_MasterList.memberfunction in the constructor the database is open and memberfunction can run normally, but when i call m_MasterList.memberfunction in a memberfunction of Cmain, then m_db.isOpen() returns false?I have put a breakpoint at m_db.close(); wich confirms that the close function is only called when i close the application.
I have no idea why the database closes before i close the ui?
-
Hi and welcome to devnet,
First thing first: please follow the documentation and do not store a QSqlDatabase object as class member. There's no need for that.
Next thing is that you don't show how you manage that database connection so there's no real way to guess what is happening with it.
-
Hi and welcome to devnet,
First thing first: please follow the documentation and do not store a QSqlDatabase object as class member. There's no need for that.
Next thing is that you don't show how you manage that database connection so there's no real way to guess what is happening with it.
@SGaist
Hi !
Thank you, the documentation is unfortunatelly not clear enough. Can i see some more examples somewhere ?
Maybe the connection loss have something to do with the QTemporaryFile where i store the database in ...If i could find more clear information about how and when to use QTemporaryFile for databases, i think i can figure out how to do this properly.
-
Why a QTemporaryFile ?
It's gone as soon as closed and this will also happen when the object goes out of scope.If you want to use a temporary database, you can use the in-memory mode of SQLite.
As for examples, there are several in Qt's documentation.