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

As a developer, I want to expose non-QObject data structures to QJSEngine

    XMLWordPrintable

Details

    • 2b5fb185627f8adfb6c5b3d62990a58429bf4ea7 (qtdeclarative)

    Description

      In QtScript there’s the qScriptRegisterMetaType() function that registers custom types using custom conversion functions. This would allow me to register the following class for example:

      class Point3D {
       
          public:
              int x;
              int y;
              int z;
       
              Point3D() = default;
              Point3D(int x, int y, int z);
       
              bool operator==(const Point3D &other) const;
              bool operator!=(const Point3D &other) const;
       
              Point3D operator+(const Vector3D &vector) const;
              Point3D operator-(const Vector3D &vector) const;
       
              Vector3D operator-(const Point3D &other) const;
       
              QString toString() const;
       
              static QScriptValue toScriptValue(QScriptEngine *engine, const Point3D &point);
              static void fromScriptValue(const QScriptValue &object, Point3D &point);
      };
      

      The last two functions were passed to qScriptRegisterMetaType() and would do the actual conversion, which in this case converted the point to a JS array with 3 elements. Important to note is that the Point3D class does not inherit from QObject; it’s a POD.

      I believe the above use-case should be enabled using QJSEngine.

      Converting all types to QObject is really not an option for me and to make matters worse, I even have some QObject-based classes that will still need custom conversion functions as the conversion is more complex than a simple call to QJSEngine::newQObject(). Therefore I really need an equivalent to qScriptRegisterMetaType() that will work with QJSEngine.

      Attachments

        Issue Links

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

          Activity

            People

              shausman Simon Hausmann
              arendjr Arend van Beelen
              Votes:
              10 Vote for this issue
              Watchers:
              18 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes