[SOLVED] create object dimanically problem
-
I want to create dynamically qml objects, from another qml file with javascript. Problem: "that pressing a button on the newly created buttons rise to new text from Array (myCars[])".
I want to do when i pressed the button named "Array" created new objects (buttons) has a different text from Array (MyCars[]).
Thanks for any help. ;)
These are files:
My main.qml file:
@import QtQuick 2.0
import QtQuick.Controls 1.0
import "componentCreation.js" as MyScriptRectangle {
id: appWindow
width: 300; height: 300Item { id: container width: 60; height: 400; anchors.horizontalCenter: parent.horizontalCenter; Text { id: spp; } } Button { text: "Array"; opacity: 80; onClicked: { MyScript.createSpriteObjects(); } }
}@
Sprite.qml (button created as object):
@import QtQuick 2.0
import "componentCreation.js" as MyScriptRectangle {
id: button;
anchors.horizontalCenter: parent.horizontalCenter;
width: 50;
height: 20;
color: "green"MouseArea { anchors.fill: parent onClicked: { console.log("Clicked"); } }
}@
Object create function "componentCreation.js":
@function createSpriteObjects() {
var component = Qt.createComponent("Sprite.qml");
var myCars=new Array("A","B","C");
var sprite;for (var i=0; i<myCars.length; i++) { sprite = component.createObject(container); sprite.y = (sprite.height + 15) * i; spp.text = myCars[i]; }
}@
-
Hi,
So far i have understood from your explanation is you want to set text (viz. "A","B" or "C") from Array inside the Sprite.qml from the "componentCreation.js"
If that is the case the you need to declare an alias in Sprite.qml and then you can access that alias from the js.
So add this in Sprite.qml at the top
@
property alias customtext: button.mytext
property string mytext
@then add a Text element in the same as,
@
Text {
id: txt
text: mytext
}
@then access the "customtext" property from js as,
@
sprite.customtext = myCars[i];
@Hope this helps a little.
-
It works, very thanks you for the reply. :)
-
You're welcome :) You can mark the thread as solved. Edit the title of this thread an prepend [solved].