Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Singleton on Qt.

Singleton on Qt.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 7.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcbasso
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Scylla
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dcbasso
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Not working how? Do you get the same error?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dcbasso
            wrote on last edited by
            #5

            Yes, same error!

            1 Reply Last reply
            0
            • F Offline
              F Offline
              franku
              wrote on last edited by
              #6

              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.

              This, Jen, is the internet.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dcbasso
                wrote on last edited by
                #7

                franku, I will try change as you write.

                Thanks.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dcbasso
                  wrote on last edited by
                  #8

                  @
                  ...
                  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!

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dcbasso
                    wrote on last edited by
                    #9

                    This fix my problem!
                    Thanks!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved