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. Is there any way to change QJSEngine object ownership?

Is there any way to change QJSEngine object ownership?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.2k 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.
  • S Offline
    S Offline
    shiro_limit
    wrote on last edited by
    #1

    Good day!

    I'm developing an application with wide scripting capabilities using QJSEngine as JS script engine.
    There is singleton class (inherited from QObject, and NULL parent) that needs to be available from multiple instances of QJSEngine class.

    If I bind my singleton class instance to one of the JS engine instance like this:

    @auto jsSingleton = _jsEngine.newQObject(Singleton::instance());
    _jsEngine.globalObject().setProperty("singleton", jsSingleton );@

    then my singleton class instance will be deleted during corresponding QJSEngine destructor.

    Is there any way to prevent it, like explicitly set JS engine object ownership to CppOwnership?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shiro_limit
      wrote on last edited by
      #2

      It looks like I found an answer, but it's kinda strange.

      1. include <QQmlEngine> instead of <QJSEngine>
      2. after setting property of global object call this:
        @QQmlEngine::setObjectOwnership(Singleton::instance(), QQmlEngine::CppOwnership);@

      And now my singleton object is not deleted.

      It looks like this method should be moved from QQmlEngine to QJSEngine, because it internally uses only QQmlData object and has nothing specific to QQmlEngine.

      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