Unsolved What is the best way sharing a QSqlDatabase
-
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..
-
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_InitializationThe (ugly) way is using
a singleton.
http://www.yolinux.com/TUTORIALS/C++Singleton.htmlNot sure what you are after?
Only share it to forms ? -
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.
-
Thanks mrjj,
Is there any RAII example of QSqlDatabase usage...
Thanks..
-
You have several examples related to the QSql module and its uses here