Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlDatabase database seems not to stay open

QSqlDatabase database seems not to stay open

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 366 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Florida
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        F Offline
        F Offline
        Florida
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved