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. QmlSingletonRegisterType and QmlSingletonRegisterIstance

QmlSingletonRegisterType and QmlSingletonRegisterIstance

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 105 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.
  • S Offline
    S Offline
    Shrishashank
    wrote 20 days ago last edited by Shrishashank
    #1

    what is the difference between qmlregisterSingletotype and qmlregisterSingletoinstance...?
    I didn't see much key diffrence as I go through documentation .Please help me to know the key difference ...

    SHRISHASHANK S K

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote 19 days ago last edited by
      #2

      Hi,

      From a quick look, the former is to registry a QML type while the latter is to register an actual C++ object instance.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bob64
        wrote 19 days ago last edited by
        #3

        The important thing to understand is that the "singleton" that QML knows about does not necessarily have to be a singleton on the C++ side. You might already have an object that you want to make available to QML using the singleton access syntax but the object already exists and is not a singleton in the usually understood C++ sense. I find this useful because typically my C++ classes have dependencies that need to be passed in at construction, whereas qmlRegisterSingletonType creates the object for QML when it is needed and doesn't provide an easy means to pass in constructor arguments.

        Having said that, I believe that using qmlRegisterSingletonInstance is discouraged these days and that a certain combination of macros should be used to achieve a similar thing, but I have not switched over to this yet.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          FKosmale
          wrote 16 days ago last edited by
          #4

          The existing answers are correct, but to amend them: With qmlRegisterSingletonInstance, the same instance is shared by all engines (which can be somewhat problematic when there's more than one), whereas with qmlRegisterSingletonType, each engine will end up with its own instance.

          Please see also https://doc.qt.io/qt-6/qml-singleton.html , especially the sections on "Exposing an existing object as a singleton", "Imperative type registration" and "Guidelines for (not) using singletons".

          1 Reply Last reply
          1

          1/4

          28 Apr 2025, 12:22

          • 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