Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create Singleton usable in c++ & qml
Forum Updated to NodeBB v4.3 + New Features

Create Singleton usable in c++ & qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 1.3k 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.
  • R Offline
    R Offline
    Redman
    wrote on 17 Jun 2024, 08:04 last edited by
    #1

    I have a Logger class. This class should be available from C++ and Qml and should be a singleton.
    All preparations like QML_SINGLETON etc. are made.

    How can I create my Logger singleton in my main.cpp and use the same instance in my application.qml?
    My Logger is running in its own thread.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bob64
      wrote on 17 Jun 2024, 09:13 last edited by
      #2

      You should probably use qmlRegisterSingletonInstance or maybe qmlRegisterSingletonType rather than the QML_SINGLETON macro. I think this will give you the control you need.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Johan_R
        wrote on 18 Jun 2024, 12:37 last edited by Johan_R
        #3

        First you define the singleton in C++. The best way is to use the Meyer's Singleton pattern. Do not use QML_SINGLETON . Then you export the singleton to QML by calling qmlRegisterSingletonInstance() in your main() function.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          Ulf Hermann
          wrote on 18 Jun 2024, 13:03 last edited by
          #4

          QML singletons are not the same as C++ singletons. The most important difference is that each QML engine gets its own instance of each singleton. It has been like that since the introduction of singletons in QML. The qmlRegisterSingletonInstance() method was mistake because it doesn't (and cannot) specify the engine the singleton instance belongs to. We shouldn't have added it. There is one way to work around this if you have only a single engine: https://doc.qt.io/qt-6/qml-singleton.html#exposing-an-existing-object-as-a-singleton

          1 Reply Last reply
          2
          • B Offline
            B Offline
            Bob64
            wrote on 19 Jun 2024, 09:02 last edited by
            #5

            I am still using Qt 5 and am trying to adopt the modern approach and move away from context properties - it seemed that this is what we are encouraged to do. However, this issue with qmlRegisterSingletonInstance does not appear to have been explained in the Qt 5 documentation. Having tried to do the right thing, it appears that I am now faced with new complications that are emerging out of the woodwork.

            If we shouldn't use qmlRegisterSingletonInstance, how are we supposed to provide the dependencies needed by the C++ object? That is, if I want to expose a C++ object as a QML singleton, and it doesn't make sense to default construct that object, what is the recommended approach?

            1 Reply Last reply
            0

            1/5

            17 Jun 2024, 08:04

            • Login

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