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. Sharing a singleton object in C++ and QML: am I on the right way ?
Forum Updated to NodeBB v4.3 + New Features

Sharing a singleton object in C++ and QML: am I on the right way ?

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • GianlucaG Offline
    GianlucaG Offline
    Gianluca
    wrote on last edited by VRonin
    #1

    Dear all,
    I need to share a singleton object both in C++ and QML. Hence, there is only one instance of an object that can be accessed from C++ side and from QML script.
    For doing so, I registered the singleton object using the qmlRegisterSingletonType and implemented the singleton provider function returning the instance that I use on C++ side.
    That's the code:

    QObject* QFacebook::qFacebookProvider(QQmlEngine *engine, QJSEngine *scriptEngine) {
     Q_UNUSED(engine)
     Q_UNUSED(scriptEngine)
     return QFacebook::instance();
    }
    
    QFacebook* QFacebook::instance() {
     static QFacebook* facebook = new QFacebook();
     return facebook;
    }
    

    But, I don't know if in this way I can break some rules about QML object allocation. In the documentation is clearly stated that the object returned by singleton provider is owned by QML. So, this imply that QML may destroy the singleton instance ? If so, why QML should destroy a singleton ?
    What are the problems of having a singleton instance owned by QML and shared into C++ side ?

    Thanks,
    Gianluca.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TopNotch_Mach07
      wrote on last edited by TopNotch_Mach07
      #2

      Hi Gianluca,

      Have you got any solution to your problem? I am facing exactly the same issue.

      Thanks,
      Thawfeek.

      E 1 Reply Last reply
      0
      • T TopNotch_Mach07

        Hi Gianluca,

        Have you got any solution to your problem? I am facing exactly the same issue.

        Thanks,
        Thawfeek.

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

        @TopNotch_Mach07 First make sure you really need such a singleton. Do your requirements include something mentioned in "Registering Singleton Objects with a Singleton Type" (http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-c-types-with-the-qml-type-system)? Could you replace it with just an object set as a context property?

        1 Reply Last reply
        0

        • Login

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