[Interest] Replacing QScript with QML
Ian Geiser
geiseri at geekcentral.pub
Wed Feb 15 20:50:02 CET 2017
---- On Wed, 15 Feb 2017 13:55:33 -0500 Jérôme Godbout <jerome at bodycad.com> wrote ----
> You could create a singelton Factory that will create the objects into javascripts and wrap the whole creation into a simple functions, you can also make a map from object name to the actual Qml Component. Just make sure you give the object the proper parenting and ownership and delete them properly.
> // MyFactorySingelton.qmlpragma Singletonimport QtQuick 2.3
> QmlObject{ id: component readonly property int objUno: 0 readonly property int objTwo: 1
> readonly property var objType: [ 'objUno' , 'objTwo' ] readonly property var objComponent: component.revaluate.concat( [ Qt.createComponent('ObjectUno.qml', Component.PreferSynchronous, component) , Qt.createComponent('ObjectTwo.qml', Component.PreferSynchronous, component) ]) function createByName(name, parent, initMap) // ex: createByName('objUno', myParent, { 'width': 500, 'height': 200 }) { return createById(component[component.objType[name]], parent, initMap); } function createById(obj_index, parent, initMap) // ex: create(1, myParent, { 'width': 500, 'height': 200 }) { return objComponent[obj_index].create(parent, ); } function create(comp, parent, initMap) { return comp.createObject(parent, initMap); }}
> you can use it into your javascript .import MyFactoryModule 0.1 as MyFactory
> or qml script easily after thatimport MyFactoryModule 0.1 MyFactorySingelton.createByName('objUno')
>
> If you are looking at only kicking some script, Component.onCompleted or use the WorkerScript to kick javascript only scripting:http://doc.qt.io/qt-5/qml-workerscript.html
My problem is that I only have C++ objects I want to create. I looked at a "factory", but that meant I needed to not only register the types, but build up a map and expose that. Too bad there wasn't a way to even do a Foo.createObject(...) from a component Foo that was defined in C++.
More information about the Interest
mailing list