Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-78066

Make QJSEngine support for overriding JavaScriptOwnership

    XMLWordPrintable

Details

    • All
    • 1f39b8ba2745f2b9ff462d40f6b0ca473c94484e

    Description

      For example, I have a QJSEngiene and I want to expose my class Foo to QJSEngine.

      class Foo : public QObject
      {
      public:
          Foo();
          Q_INVOKABLE void someFunction();
      
      };
      
      class Bar
      {
      public:
          MyClass() {
              _foo1 = new Foo();
      	QJSValue jsFoo1 = _jsEngine.newQObject(_foo1);
              QJSValue jsFoo2 = _jsEngine.newQObject(&_foo2);
      	_jsEngine.globalObject().setProperty("foo1", jsFoo1);
              _jsEngine.globalObject().setProperty("foo2", jsFoo2);
          }
          ~MyClass() {
              // Here I don't delete _foo1, because It's owned by _jsEngine.
          }
      
      private:
          QJSEngine _jsEngine;
          Foo* _foo1;
          Foo _foo2;
      
      }
      

      Code description

      Here, following the documentation, the _jsEngine takes the ownership of both objects. Which leeds to a problem when the Bar is destroyed.

      The obejct _foo1, which is created dynamically, will be destroyed by QJSEngine, and I can skip deleting it in the desturctor of Bar.

      But I can't skip the destroying of _foo2, which is created statically.

       

      I understand that I can manage this question, just creating all objects dynamically. But then I have to handle in my head that "I must not delete it, because it's passed to QJSEngine somewhere else in my code".

      And there even can be cases, when creating the object dynamically is undesirable or not possible.

      Suggestion

      QQmlEngine, which inherits QJSEngine, has a convinient method setObjectOwnership() to handle this quiestion.

      I think that it's a good idea to make an option for user to force CppOwnership.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              sirotin Igor Sirotin
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes