PDA

View Full Version : QSqlDatabase database seems not to stay open



floridiaan
29th December 2022, 16:25
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?

d_stranz
30th December 2022, 18:17
I have no idea why the database closes before i close the ui?

Without the code that actually shows what you are doing, it is impossible to tell what is wrong.

Are you sure your have not "shadowed" m_db by declaring another local variable of that name in your constructor that hides (shadows) the member variable declared in the class? (Or likewise, the same for m_MasterList?) That is one thing that could cause such behavior.

sophvic
4th January 2023, 11:12
Hard to tell without much information.

But my first impression is you are accessing the m_db from different QThread.