Dynamic QML object question
-
I can create the qml object using Qt.createQmlObject, but how do I reference the object in javascript if I want to change it's properties.
Thanks
-
Hi,
The object which is created will be a QtObject-derived type. So you can reference it with a QtObject property, or (if you're using Qt5) a var property. (Note: you can also use a variant property, but I really suggest not doing that).
eg:
@
Item {
id: root
property QtObject objref
Component.onCompleted: {
objref = Qt.createQmlObject("import QtQuick 2.0; Item { }", root)
setPropsFunc()
}function setPropsFunc() { objref.width = 100 objref.height = 100 // etc }
}
@Cheers,
Chris. -
Thanks for the reply, that helped a lot.
What if I was to use the code below to create my keyboard dynamically:
@
var keyboardObject = Qt.createObject(root, {"z": 30, "keyHeight": 40, "keyWidth":28 });
@This is what it looks like if the keyboard is just added to the QML file statically:
@
VirtualKeyboard {
id: vk
z: 30
keyHeight: 40
keyWidth: 28onButtonPressed: { Ops.doTextOperation(op) } }
@
How can I add the onButtonPressed event dynamically using Qt.createObject? Maybe it can't be done.
-
With Qt5 or Qt4.x?
With Qt5 I think you could probably use a property var and initialise it with an array containing a function value (to avoid it being considered a binding).
eg:
@
VirtualKeyBoard {
property var onClickedFunction
// ...
}var keyboardObject = Qt.createObject(root, {"onClickedFunction": [function(){console.log("hi")}]});
@Cheers,
Chris. -
I'm using 4.8.4. I don't think it can be done. There doesn't seem to be a way I can trap that event when I create the keyboard dynamically.