I have a QSqlDatabase database main object in QMainWindow. This object is used to access all data (even secret). But to this objects must have access to derivatives (child windows). How to do it?

  • one
    It is necessary to independently search for a solution on the Internet before asking a question on any resource. - Victor Borovlev

2 answers 2

Once the connection is established, you can call the static function QSqlDatabase :: database () from anywhere in the program with the name of the connection to get a pointer to this connection. If you do not pass the name of the connection, it returns the default connection.

original

    Hello again)

    For example, you can use Singleton, which will be responsible for this object.

    class DBProvider { public: static QSqlDatabase& database() { return DBProvider::instance()->m_database; } private: QSqlDatabase m_database; static DBProvider& instance() { static DBProvider dbPr; dbPr.m_database = QSqlDatabase::database("connectname123", true); return dbPr; } DBProvider() { //подключаетесь к базе: m_database = QSqlDatabase::addDatabase(..., "connectname123"); } ~DBProvider() { } DBProvider(DBProvider const&); DBProvider& operator= (DBProvider const&); };