[solved] How can I make something like Qt.createComponent(Rectangle) ?
-
I want a Rectangle created from javascript. I'm not proficient with Qt. I can't do that in QML right? Only if I use @createQmlObject@.
Also, how can I access the Qt Quick Components that I have in my QTDIR\imports folder? Something like
@Qt.createComponent(Qt.SystemVariable("QTDIR") + "\imports\myPlugin\MyComponent.qml")@
Thanks
-
You can create QML objects from JavaScript. For example, this create 10 Rectangle elements positioned manually on a diagonal.
@import QtQuick 1.0
Item {
width: 640
height: 480Item { function createItems() { var itemList = [] for (var i = 0; i < 10; ++i) { itemList[i] = Qt.createQmlObject( 'import QtQuick 1.0; Rectangle { width: 10; height: 10;' + ' x: ' + i*20 + '; y: ' + i*20 + '; color: "blue" }', parent, "createItems()"); } return itemList } property variant items: createItems() }
}@
To access a plugin use, assuming the plugin is properly setup.
@import myPlugin 1.0@
-
Copy the following code and put them together in the same dir
MyJava.js
@var component;function createMyRect(vWidth,vHeight) {
if (component == null)
component = Qt.createComponent("MyRect.qml");if (component.status == Component.Ready) { var dynamicObject = component.createObject(myrect); if (dynamicObject == null) { console.log("error creating Myrect"); console.log(component.errorString()); return false; } dynamicObject.width= vWidth; dynamicObject.height = vHeight; dynamicObject.color = "red"; } else { console.log("error loading block component"); console.log(component.errorString()); return false; } return true;
}
@the second : MyRect.qml
@import Qt 4.7
import "MyJava.js" as JavaCodeRectangle{
id:myrect
color:"lightgreen"width:200; height:200; MouseArea{ anchors.fill:parent; onClicked:JavaCode.createMyRect(100,100); }
}
@ -
Alright, thanks for the ideas