Singleton on Qt.



  • I have create a Singleton Class to handle with my DataBaseConnection, see:

    SingletonDatabaseConnection.h
    @
    #ifndef SINGLETONDATABASECONNECTION_H
    #define SINGLETONDATABASECONNECTION_H

    #include <QMutex>
    #include "QtSql/QtSql"

    class SingletonDatabaseConnection
    {
    public:
    static SingletonDatabaseConnection* getInstance()
    {
    static QMutex mutex;
    if (!myInstance) {
    mutex.lock();
    if (!myInstance) {
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
    if (database.open()) {
    // database.exec("");
    readCreateTable();
    }
    myInstance = new SingletonDatabaseConnection();
    }
    mutex.unlock();
    }
    return myInstance;
    }

    static void drop()
    {
        static QMutex mutex;
        mutex.lock();
        delete myInstance;
        myInstance = 0;
        mutex.unlock();
    }
    
    void openDatabase();
    QSqlDatabase getDatabase();
    

    private:
    static SingletonDatabaseConnection* myInstance;
    static QSqlDatabase database;

    SingletonDatabaseConnection();
    SingletonDatabaseConnection(const SingletonDatabaseConnection &);
    SingletonDatabaseConnection& operator=(const SingletonDatabaseConnection &);
    
    static void readCreateTable()
    {
       QFile file&#40;":/database/CreateTable.sql"&#41;;
       if(!file.open(QIODevice::ReadOnly))
       {
           qDebug() << "error opening file: " << file.error();
           return;
       }
       QTextStream instream(&file);
       QString line = instream.readLine();
       qDebug() << "first line: " << line;
       file.close();
    }
    

    };

    #endif // SINGLETONDATABASECONNECTION_H
    @

    SingletonDatabaseConnection.cpp
    @
    #include "singletondatabaseconnection.h"

    //SingletonDatabaseConnection::SingletonDatabaseConnection()
    //{
    //}
    SingletonDatabaseConnection* SingletonDatabaseConnection::myInstance = 0;
    @

    When I try to use this Singleton on my mainWindow, the compiler gives me a error:

    @
    void MainWindow::on_pushButton_clicked()
    {
    SingletonDatabaseConnection *singleton = SingletonDatabaseConnection::getInstance();
    // singleton->getDatabase();

    }
    @

    Error:
    "undefinded reference to "QsqlDataBase::defaultConnection""
    .... any other similar too...



  • May be you miss "QT += sql" in your *.pro file?



  • Yes it's missing... But still not working!



  • Not working how? Do you get the same error?



  • Yes, same error!



  • Some changes, as the QSqlDatabase already contains static funtionality. In my opinion it is not neccessary to implement your class as a singleton since it could be static at all:

    Private constructor is missing its implementation (used by the new operator):
    @ SingletonDatabaseConnection() {}@

    Do not try to store the QSqlDatabase object as a static variable:
    @static QSqlDatabase database; //remove this line@

    Instead use QSqlDatabase as already static class:
    @QSqlDatabase getDatabase() { return QSqlDatabase::database(); }@

    Change the initialization into:
    @
    ...
    if (!myInstance) {
    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("inoveDB.db3");
    if (database.open()) { ...
    @

    This is already done in the initialization of the Singleton, so don't do it twice:
    @void openDatabase(); @

    Return the singleton already provided by the QSqlDatabase (you currently only use the default connection):
    @QSqlDatabase getDatabase() { return QSqlDatabase::database(); }@

    If you want to check easily if this is working, implement the following method that you call in the button-slot:
    @QString getDatabaseName() { return getDatabase().databaseName(); }@

    The database is closed when the application is finished. But you might want to close it manually in the drop() function.



  • franku, I will try change as you write.

    Thanks.



  • @
    ...
    if (!myInstance) {
    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("inoveDB.db3");
    if (database.open()) { ...
    @

    Where I need to put that?
    I change the method getDatabase(), appers to resolve the problem... I'm need to make this works fine.

    FOGET THIS!



  • This fix my problem!
    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.