Quantcast
Channel: Installation and Deployment
Viewing all articles
Browse latest Browse all 22112

Custom C++ types and QJSEngine

$
0
0
Hi there, I’m converting a project from using QtScript to the new declarative engine. My goal is to be able to utilize QML when this is done, and use the same script context for the existing scripts and the QML. I’ve been able to convert pretty much everything relatively easily, but there’s one thing I cannot find out. 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. Does anyone know if this is possible to achieve with QJSEngine as well? The only relevant function I found thus far — qmlRegisterType() — only works with QObject-based classes. 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(). Thanks! Arend jr. PS.: I’d really wish some of the more advanced options of QScriptEngine::newQObject() were available with QJSEngine::newQObject() as well…

Viewing all articles
Browse latest Browse all 22112

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>