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. Are singletons considered good or bad for some config
Forum Updated to NodeBB v4.3 + New Features

Are singletons considered good or bad for some config

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 2.1k Views 4 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.
  • M Offline
    M Offline
    mpergand
    wrote on last edited by mpergand
    #4

    In a QApplication subclass add any method you need.
    You can call this method anywhere:

    class MyApplication : public QApplication
    {
    public:
    static MyApplication*	instance() { return static_cast<MyApplication*>(QApplication::instance()); }
    
    Database* database { return mDatabase; }
    
    private:
    
    Database* mDatabase;
    }
    

    Anywhere in your App
    #include "MyApplication"

    Database* database=MyApplication::instance()->database();

    Thank YouT 1 Reply Last reply
    0
    • M mpergand

      In a QApplication subclass add any method you need.
      You can call this method anywhere:

      class MyApplication : public QApplication
      {
      public:
      static MyApplication*	instance() { return static_cast<MyApplication*>(QApplication::instance()); }
      
      Database* database { return mDatabase; }
      
      private:
      
      Database* mDatabase;
      }
      

      Anywhere in your App
      #include "MyApplication"

      Database* database=MyApplication::instance()->database();

      Thank YouT Offline
      Thank YouT Offline
      Thank You
      wrote on last edited by
      #5

      @mpergand

      I have some questions?

      Which approach is better?
      Why one over another?

      Let's make QT free or It will go forever

      TRUE AND FALSE <3

      M 1 Reply Last reply
      0
      • Thank YouT Thank You

        @mpergand

        I have some questions?

        Which approach is better?
        Why one over another?

        M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #6

        In your case, i don't see any good reason for not using a singleton.

        This class is used in 10 files

        The question is why do you need to access the database in so many places.

        Thank YouT 1 Reply Last reply
        1
        • M mpergand

          In your case, i don't see any good reason for not using a singleton.

          This class is used in 10 files

          The question is why do you need to access the database in so many places.

          Thank YouT Offline
          Thank YouT Offline
          Thank You
          wrote on last edited by
          #7

          @mpergand
          I wanted to know about this.
          I have been told to use only one connection in this forum multiple times. But also I wanted to know how we can do this problem

          Let's make QT free or It will go forever

          TRUE AND FALSE <3

          jsulmJ 1 Reply Last reply
          0
          • Thank YouT Thank You

            @mpergand
            I wanted to know about this.
            I have been told to use only one connection in this forum multiple times. But also I wanted to know how we can do this problem

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @Thank-You https://doc.qt.io/qt-5/qsqldatabase.html already manages all your connections for you and you can get them at any place in your app using https://doc.qt.io/qt-5/qsqldatabase.html#database
            There is absolutely no need to make your class a singleton.
            Please take time to read https://doc.qt.io/qt-5/qsqldatabase.html - it shows how to do it properly and it also explains that you should not keep any QSqlDatabase instances as variables.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #9

              There is one major problem with singletons: they get in the way of testing. It is easier when your function gets handed over an object instead of querying a singleton. In testing you can then easily exchange this object. You can still have a default parameter for this object which will pass in a default object. But, since you can have a default object plus other objects for tests, it is technically not a singleton anymore.

              Another minor problem with singletons is the way they used to be implemented: as global variable (sometimes a global static class variable). Since processors got multiple cores and people started using threads more this introduces problems with the initialization order. In modern C++ you can use a static variable inside a function. The standard guarantees that it will only be initialized once even in multithreaded contexts. This is still relevant if you are planning on using a default object as default parameter in function calls just as I have explained in the first paragraph.

              Still, in your concrete case you should have a look at @jsulm's answer.

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mpergand
                wrote on last edited by
                #10

                As an alternative, here a template class that manege a single instance of a object:

                #define INITIALIZE_ONCE static bool _init_=false;    \
                                        if(_init_) return; _init_=true;
                  
                template<class T>
                class Single
                {
                    public:
                        
                        Single()
                            {
                            INITIALIZE_ONCE;
                            sSingle=new T;
                            qDebug()<<"sSingle="<<sSingle;
                            }
                        
                    T* operator->()
                            {
                            return sSingle;
                            }
                    
                    
                    T* operator *()
                            {
                            return sSingle;
                            }
                    
                   
                    private:
                        
                        static T* sSingle;
                };
                
                template<class T>
                T* Single<T>::sSingle=nullptr;
                
                
                class Add
                {
                    public:
                        
                        int add()
                            {
                            i++;
                            return i;
                            }
                        
                    private:
                        int i=0;
                };
                
                int main(int argc, char *argv[])
                {    
                    Single<Add> ss;
                    Add* s=*ss;  // return the single instance
                    qDebug()<<"sSingle="<<s;
                    
                    qDebug()<<Single<Add>()->add();
                    qDebug()<<s->add();
                    qDebug()<<ss->add();
                    
                    using A=Single<Add>;
                    A a;
                    qDebug()<<A()->add();
                    qDebug()<<a->add();
                    
                  return 0;
                }
                
                1 Reply Last reply
                0
                • Thank YouT Offline
                  Thank YouT Offline
                  Thank You
                  wrote on last edited by
                  #11

                  I respect every answer.
                  @jsulm had already answered how to handle this case here
                  https://forum.qt.io/topic/131013/headers-in-c and I asked it there too
                  I had implemented it in that project in main QApplication(). I guess and hope it was correct to connect database at the very beginning of program.

                  There he had mentioned about Singletons https://forum.qt.io/topic/131013/headers-in-c/5
                  So I wanted to know how it does and work and is it good or not.
                  But I wanted to know other methods only

                  A kinda silly question
                  How can we get the same way if there was no QSqlDatabase?
                  If it's too bad just ignore this one LOL

                  Thank you for your all answers

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  kshegunovK 1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @Thank-You said in Are singletons considered good or bad for some config:

                    How can we get the same way if there was no QSqlDatabase?

                    For example with Q_GLOBAL_STATIC - it's thread-safe (in contrast to @mpergand's solution)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • Thank YouT Thank You

                      I respect every answer.
                      @jsulm had already answered how to handle this case here
                      https://forum.qt.io/topic/131013/headers-in-c and I asked it there too
                      I had implemented it in that project in main QApplication(). I guess and hope it was correct to connect database at the very beginning of program.

                      There he had mentioned about Singletons https://forum.qt.io/topic/131013/headers-in-c/5
                      So I wanted to know how it does and work and is it good or not.
                      But I wanted to know other methods only

                      A kinda silly question
                      How can we get the same way if there was no QSqlDatabase?
                      If it's too bad just ignore this one LOL

                      Thank you for your all answers

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #13

                      @Thank-You said in Are singletons considered good or bad for some config:

                      So I wanted to know how it does and work and is it good or not.
                      But I wanted to know other methods only

                      I was intending not to take part in this one, but what can I say, I'm a masochist.

                      The Qt wayâ„¢ is what Christian posted, however his comment is true only partially. The initialization of the global is thread-safe, the object behind is not. So if one assumes that the variable is going to be used from multiple threads then making the construction/destruction thread-safe is only half the job.

                      So I'll preach what I usually preach: don't use a singleton if you can avoid it. It's more trouble than it's worth, and ultimately it's worth very little to begin with.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      3

                      • Login

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