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. Singletons for small UIs (QML based). To use or not to use?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
singleton
4 Posts 3 Posters 746 Views
  • 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.
  • A Offline
    A Offline
    Ankit.Jain
    wrote on 28 Jan 2019, 07:08 last edited by
    #1

    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.

    J J 2 Replies Last reply 28 Jan 2019, 08:11
    0
    • A Ankit.Jain
      28 Jan 2019, 07:08

      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.

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 28 Jan 2019, 08:11 last edited by
      #2

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

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

      A 1 Reply Last reply 28 Jan 2019, 08:54
      0
      • A Ankit.Jain
        28 Jan 2019, 07:08

        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.

        J Online
        J Online
        J.Hilk
        Moderators
        wrote on 28 Jan 2019, 08:34 last edited by
        #3

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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • J jsulm
          28 Jan 2019, 08:11

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

          A Offline
          A Offline
          Ankit.Jain
          wrote on 28 Jan 2019, 08:54 last edited by
          #4

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

          1 Reply Last reply
          0

          1/4

          28 Jan 2019, 07:08

          • Login

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