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. Accessing Singleton from across the Qt app

Accessing Singleton from across the Qt app

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 6.6k Views 2 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.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by
    #1

    Hi,

    I'm still in my learning process of both C++ and Qt. Recently I learned the Singleton pattern and tried out an example with a few modification to the code. I tested the code as a plain C++ program and it worked as expected. I now want to make this singleton be part of my Qt app. Where should I create/initialize the singleton to be able to access the same instance from across the Qt app.

    I used qt designer to create my app. It has main, mainwindow and several Qdialogs. I want to be able to access the Singleton instance from all of them.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      You access your singleton class in a C++ program using Qt exactly the same way as in a C++ program that does not use Qt: through its static "instance" function. That function will either cause the instance to be created, or provided a pointer/reference to the one that already exists.

      W 1 Reply Last reply
      0
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @WhatIf
        My advice: just don't use the singleton anti-pattern!
        Here's some relevant discussions:
        http://forum.qt.io/topic/64346/qtextdocument-and-multithreading
        http://forum.qt.io/topic/62652/const-global-variable-outside-of-constructors
        http://forum.qt.io/topic/62451/message-handler-crashes-if-installed-on-sepparate-class

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • C ChrisW67

          You access your singleton class in a C++ program using Qt exactly the same way as in a C++ program that does not use Qt: through its static "instance" function. That function will either cause the instance to be created, or provided a pointer/reference to the one that already exists.

          W Offline
          W Offline
          WhatIf
          wrote on last edited by
          #4

          @ChrisW67

          Well, in my plain c++ program, the only place where I accessed the singleton was from inside the main function.

          Now, in a Qt program, I will have to access the singleton from mainwindow and a few QDialogs. If I include (#include "Singelton") in mainwindow and the few QDialogs, won't that create multiple objects of the singleton each has a single instance. For example,

          1. I call, Singleton::getInstance()->anyFunction();, from mainwindow, if it's the first code to call getInsatnce(), the instance (instance #1) will be created and only that single instance will be used anywhere inside of mainwindow.

          2. I call, Singleton::getInstance()->anyFunction();, from any QDialog, if it's the first code to call getInsatnce(), the instance (instance #2) will be created and only that single instance will be used anywhere inside of a specific QDialog.

          Is it one instance per class or app?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            If the singleton is programmed correctly, it is intended to be used in whole program and
            will only 1 instance of the class will exists for whole application.

            so one instance pr app.

            but as @kshegunov points out, its not a good pattern as it very near to just
            using global a variable.

            W 1 Reply Last reply
            0
            • mrjjM mrjj

              Hi
              If the singleton is programmed correctly, it is intended to be used in whole program and
              will only 1 instance of the class will exists for whole application.

              so one instance pr app.

              but as @kshegunov points out, its not a good pattern as it very near to just
              using global a variable.

              W Offline
              W Offline
              WhatIf
              wrote on last edited by
              #6

              @mrjj

              Is there another pattern that provide the same functionality the Singleton pattern does that is safer to use?

              mrjjM 1 Reply Last reply
              0
              • W WhatIf

                @mrjj

                Is there another pattern that provide the same functionality the Singleton pattern does that is safer to use?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @WhatIf
                yes you can use dependency injection
                https://en.wikipedia.org/wiki/Dependency_injection
                https://www.youtube.com/watch?v=IKD2-MAkXyQ

                It sounds so funky but in its base idea, you just give the object
                (that would have been the singleton) to the other classes that need it via
                the constructor.

                So instead of it somehow knows of a global function, u give it to the classes when u construct them. While is slightly more work, it also make it easier to
                create test version of the "service" class(es) u pass around or other such
                functions. Also u can then give different classes different version of the service if needed. something not possible with singleton.

                kshegunovK 1 Reply Last reply
                0
                • mrjjM mrjj

                  @WhatIf
                  yes you can use dependency injection
                  https://en.wikipedia.org/wiki/Dependency_injection
                  https://www.youtube.com/watch?v=IKD2-MAkXyQ

                  It sounds so funky but in its base idea, you just give the object
                  (that would have been the singleton) to the other classes that need it via
                  the constructor.

                  So instead of it somehow knows of a global function, u give it to the classes when u construct them. While is slightly more work, it also make it easier to
                  create test version of the "service" class(es) u pass around or other such
                  functions. Also u can then give different classes different version of the service if needed. something not possible with singleton.

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

                  @mrjj

                  yes you can use dependency injection
                  https://en.wikipedia.org/wiki/Dependency_injection

                  This is a thing?! Unbelievable! Everything is a pattern now. :D

                  @WhatIf
                  Do it as @mrjj suggested, it's the most clean way to accomplish what you want. Just have a set method (and/or constructor) accepting your external object and use that.

                  Read and abide by the Qt Code of Conduct

                  mrjjM 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @mrjj

                    yes you can use dependency injection
                    https://en.wikipedia.org/wiki/Dependency_injection

                    This is a thing?! Unbelievable! Everything is a pattern now. :D

                    @WhatIf
                    Do it as @mrjj suggested, it's the most clean way to accomplish what you want. Just have a set method (and/or constructor) accepting your external object and use that.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @kshegunov

                    This is a thing?! Unbelievable! Everything is a pattern now. :D

                    he he.
                    yes yes. They saw pattern putting parameters in constructors.. :)
                    They even made whole frameworks for it.
                    https://sites.google.com/site/fruitlib/

                    1 Reply Last reply
                    1
                    • R Offline
                      R Offline
                      RavensAngel
                      wrote on last edited by
                      #10

                      Look at this article.

                      1 Reply Last reply
                      2

                      • Login

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