Dynamic object's id in QML



  • i am generating new QML object with the following code:
    The polygon object is a type that i define with C++:

    @var newObject = Qt.createQmlObject("import MyTypes 1.0;"+
    "import QtQuick 1.0;"+
    "import Qt 4.7;"+
    "Polygon {"+
    "id:_"+polygonID+";"+
    //"anchors.fill:parent;"+
    "x:645; y:0;"+
    //"width: 0; height: 0;"+
    "color: "+polygonColor+";"+
    //"scale:0.3;"+
    "vertices:["+
    polygonPointsString+
    "]"+
    "Component.onCompleted:{ container.pntx= _"+polygonID+".leftAndTopOfBoundingRect().x;"+
    "container.pnty= _"+polygonID+".leftAndTopOfBoundingRect().y;"+
    "}"+
    "}",
    parentObj, "dynamicSnippet3");

    console.log"id:"+(newObject.id);
    @

    after
    @ console.log"id:"+(newObject.id);@
    line it writes:

    id:undefined

    but if i write
    @ console.log"id:"+(newObject.x);@
    line it writes:

    645 the true answer

    where am i wrong, why id is undefined?

    thanks



  • http://developer.qt.nokia.com/doc/qt-4.7/qdeclarativedynamicobjects.html#id-fc2906d7-a32a-4875-a420-59d951346a29

    Also, note that while dynamically created objects may be used the same as other objects, they do not have an id in QML.



  • thanks, i have looked at that page but did not see that line.
    if so how can i access to that objects?
    @Scale {
    id: scaleItem
    origin.x: 0; origin.y: 0; xScale: 1.2; yScale:1.2
    }@
    i must access this element,and assign it to "transform" variable of my component and i think i can do it using id, if there is no id, how?
    thanks very much for the reply



  • You use variable of that dynamically created element (newObject.transform ).



  • thanks,
    i can not try it at the moment but i need to write

    @newObject.trasnform=scaleItem;//scale item id@

    i can not do it because i will generate scale item dynamically too.

    thus can i write

    @newScaleObject=createQMLObject("import Qt 4.7; Scale{origin.x:0; blah blah}");@

    and can i write:

    @
    newObject.transform:newScaleObject@

    ?
    is it possible?



  • @var newScaleObject=createQMLObject("import Qt 4.7; Scale{origin.x:0; blah blah}");
    newScaleObject.transform = scaleItem@



  • @Diph, "scaleItem" is an id of dynamically generated object.
    thus i can not use it, system does not allow,
    anyway i will try something; hope i can succeed,

    thanks for your time @Diph and @minimoog77



  • Sorry, I didn't notice the content of the createQMLObject. Your code should work if you change the second line to newObject.transform = newScaleObject


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.