Problems with Open-Source Downloads read and

Singletons for small UIs (QML based). To use or not to use?

  • So, I have a code base where we have a couple of classes whose lifetime is as long as the code and they have a single instance.

    Now, a lot of the times, there will be some data which is required by some class which is present in a different class. At these times, the code likes to emit a signal (with a call by reference object as parameter) and the slot will basically update the parameter hence giving the emitting class the value (I didn't write this stuff, don't judge me for it).

    The whole code is essentially single threaded. In such a scenario, would it be a good idea to make these objects singleton? (the ui is in qml with backend in qt/c++). The reason I ask this is because of the bias against using this data pattern on the web.

  • Lifetime Qt Champion

    @Ankit.Jain Are these classes used at many places in your code?

  • Moderators

    I would say, that totally depends on your style,

    It's not forbidden or prevented in any case, but I would consider it bad practice.

    I personally register my c++ backend, that shares data across multiple qml files, via setContextProperty, that is in essence also a Singelton

  • @jsulm They are created inside main.cpp. Usage is high as pretty much every calculation is done in 'em.

    @J-Hilk They are currently registered via setContextProperty and can hence be used freely throughout our QML code but not the Qt/C++ part.

Log in to reply