A
A
AlexSer2019-11-22 09:59:58
Qt
AlexSer, 2019-11-22 09:59:58

How to pass a db connection to another class in QT?

How to pass a db connection to another class in QT?
Created a separate class for connecting to Sqlite DB.

SqliteConnection::SqliteConnection()
{

}

void SqliteConnection::OpenConnection(){
     db_sqlite = QSqlDatabase::addDatabase("QSQLITE");
     db_sqlite.setDatabaseName("samples.db");
     db_sqlite.open();
    if(db_sqlite.isOpen()){
        qDebug()<<"Ok";
    }
}
void SqliteConnection::CloseConnection(){
     db_sqlite.close();
}

I want to transfer connection to a DB to other classes. How to do it right?
sorry newbie. while much is not clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alex4answ, 2019-11-22
@AlexSer

Usually a class is created to work with the database, it is a singleton, and it is passed or called already in the used class.
Either some single object pool is created, and they work through it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question