lundi 30 novembre 2015

c++/Qt: How to get a reference to database in the MainWindow class?

In Qt-creator, I created SQLite database in a class called databaseManager, as follow:

QString DatabaseManager::open_db()
{
    QSqlDatabase db;
    QString path = "/Users/me/Documents/workspace/Muasaa/";
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(path+"Database v.1");
    if (db.open()){
        return "Database is created, open, and ready ...";
    } else {
        return db.lastError().text();
    }
}

Then I define the following in the header file of the MainWindow class:

Public:
   DatabaseManager *db_manager;

In the source file, I call it as follow:

db_manager->open_db();

which creates and open the database.

However, I would like to use a reference to same database to use it in many functions in the MainWindow source file. How can I do that ?!

Aucun commentaire:

Enregistrer un commentaire