What is the best way sharing a QSqlDatabase
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..
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.
The (ugly) way is using
Not sure what you are after?
Only share it to forms ?
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.
Is there any RAII example of QSqlDatabase usage...
You have several examples related to the QSql module and its uses here