Accessing Singleton from across the Qt app



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



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


  • Qt Champions 2017



  • @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?


  • Qt Champions 2017

    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.



  • @mrjj

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


  • Qt Champions 2017

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


  • Qt Champions 2017

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


  • Qt Champions 2017

    @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/



  • Look at this article.


Log in to reply
 

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