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. What is the best way sharing a QSqlDatabase
Forum Updated to NodeBB v4.3 + New Features

What is the best way sharing a QSqlDatabase

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.2k Views 2 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.
  • Z Offline
    Z Offline
    zeroptr
    wrote on last edited by
    #1

    Hi all,

    My application needs one connection to my PostgreSQL database and I wan't to share that connection to my all files and classes... I don't want that every form opens a new connection(I know QSQLdatabase keeps connection internally) or close connection when loosing scope...

    I made a lot of trying but I loved none... Any suggestion..

    Thanks..

    Linux Mint 20.04 64 Bit QT6.0.1

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

      hi
      One design would be to have QSQLdatabase in a class (like mainwin) and
      hand out references to the other objects/forms that also need to use it. (via their constructor or set method)
      That is a clean way.
      https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

      The (ugly) way is using
      a singleton.
      http://www.yolinux.com/TUTORIALS/C++Singleton.html

      Not sure what you are after?
      Only share it to forms ?

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

        Hi,

        QSqlDatabase is already sort of a singleton. The database object return for a given connection is always the same. The most widely used technique it to open the connection early as the default connection. Then you can use all the QSql related classes without doing anything special. They use the default connection unless you tell them otherwise.

        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
        1
        • Z Offline
          Z Offline
          zeroptr
          wrote on last edited by
          #4

          Thanks mrjj,

          Is there any RAII example of QSqlDatabase usage...

          Thanks..

          Linux Mint 20.04 64 Bit QT6.0.1

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

            You have several examples related to the QSql module and its uses here

            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