[Interest] Creating C++ objects in JavaScript with operator new

Jérôme Godbout godboutj at amotus.ca
Tue Apr 16 16:09:54 CEST 2019


The new doesn't allow you to set the parent of your new object, you cannot assign initial property (they are not exactly the same as setting value after). You cannot set binding upon creation of the object, you don't have previously declared context/import like the Component into which binding may already exist. There is a lot missing if you do a simple new(). Good for Qt Script, but Qt script was not doing the binding like Qml can.  Qml Script have a declarative part not only a procedural script, that part need to be known when you create the object too.

Side note:
obj.setEnabled( true );
obj.isEnabled()
are not very Qml way, they are Q_INVOKABLE method, would be better to expose a property that perform the set/get/changed
obj.enabled = true;
if(obj.enabled)
would be more Qml and reduce the clutter and leave the control on which function to use in C++ and can also perform a binding instead of value with the changed part.

-----Original Message-----
From: Richard Weickelt <richard at weickelt.de> 
Sent: April 16, 2019 7:39 AM
To: Jérôme Godbout <godboutj at amotus.ca>; interest at qt-project.org
Subject: Re: [Interest] Creating C++ objects in JavaScript with operator new

> You can also generate an object from Qml code directly: 
> Qt.createQmlObject()

I know, but I wanted to use operator new in order to achieve a better look & feel. Please note that this is/was easily possible in Qt Script. See the code snippets at https://doc.qt.io/qt-5/qtscript-index.html#making-c-class-properties-available-in-qt-script

    var obj = new MyObject;
    obj.setEnabled( true );
    print( "obj is enabled: " + obj.isEnabled() );

> The new is lacking too much information to create the QObject properly.

What do you mean and why is that?


More information about the Interest mailing list