Nominate our 2022 Qt Champions!

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 {"+
    "x:645; y:0;"+
    //"width: 0; height: 0;"+
    "color: "+polygonColor+";"+
    "Component.onCompleted:{ container.pntx= _"+polygonID+".leftAndTopOfBoundingRect().x;"+
    "container.pnty= _"+polygonID+".leftAndTopOfBoundingRect().y;"+
    parentObj, "dynamicSnippet3");


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


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

    645 the true answer

    where am i wrong, why id is undefined?



    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:


    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