Results 1 to 3 of 3

Thread: QSqlDatabase database seems not to stay open

  1. #1
    Join Date
    May 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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
    Qt Code:
    1. class CMasterList : public QObject
    2. {
    3. private:
    4. public:
    5. void memberfunction ();
    6. }
    To copy to clipboard, switch view to plain text mode 

    in cmasterlist.c i have a memberfunction
    Qt Code:
    1. void CMasterList::memberfunction ()
    2. {
    3. if(!m_db.isOpen())return;
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. class Cmain : public QMainWindow
    2. {
    3. private:
    4. CMasterList m_MasterList;
    5. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,222
    Thanks
    302
    Thanked 862 Times in 849 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSqlDatabase database seems not to stay open

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    2

    Default Re: QSqlDatabase database seems not to stay open

    Hard to tell without much information.

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

Similar Threads

  1. Console Doesn't Stay Open
    By Atomic_Sheep in forum General Programming
    Replies: 0
    Last Post: 9th October 2015, 11:09
  2. QMenu to stay open
    By xmeister in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 02:17
  3. QSqlDatabase cannot open database
    By nowrep in forum Qt Programming
    Replies: 2
    Last Post: 9th December 2011, 22:21
  4. QSqlDatabase::open Segfaults
    By rich.remer in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2010, 16:32
  5. Why QSqlDatabase::open() returns open?
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 27th August 2009, 18:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.