How to assign id to dynamically created object



  • Dear all,

    This question is originated from a previous post, which deviates from the main topic of the post.

    I have a button, which when pressed, will create an object. This object is actually a slide (if you think of it as a power point slide kind of thing), which I use qml rectangle to represent. So I am trying to give each slide an id, which is in this way: slide1, slide2, …. sliden

    I understand that we cannot dynamically assigned id to the dynamically created object.
    http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeintroduction.html
    This is super weird, if this is not possible, how are we able to reference our dynamically created object???

    But I realise that for GridView model, we can do this

    @
    GridView {
    model: TextIconModel {id:textIconModel} // declare the id of the object that you instantiated
    delegate: textIconDelegate
    }
    @

    So this is contradicting, are we able to assign id to dynamically created object?



  • The above example you have given is bit different. You are assigning a concrete TextIconModel to model which is just fine.

    I agree with you saying that there should be a way to give id to dynamically created objects. I added my rating to this :)



  • What you can do is to create a property variant variable and then assign the javascript object to that variable. Something like:

    @
    property variant foo;
    onSomethingHappened: {
    theObject = getMeTheObject();
    foo = theObject;
    }
    @



  • @sigrid, thanks for your reply. But did not get your answer. The question was is on how to assign id to a dynamically created object.



  • It is not possible to assign an id to an object. The id is part of the parsing context of the .qml file and only available inside that .qml file. The id is not accessible through JS or C++. It is possible to reference an object via its ID from JavaScript, but that is an id->name mapping that the declarative engine handles. The ID is not accessible. Hence, the second best thing is a hack that lets you access a JS created object as if it had been named, through a member property, as suggested in my previous post. Note that this is as I mentioned, a hack.



  • ok.. thanks for you clarification. Got it now. Knowing that id is not considered as the property of the object, I know that we can not assign a value to id like other properties of the qml components. But it would be very nice if we can do it somehow in the future versions of QtQuick. I hope this is on your cards???



  • [quote author="Vijay Bhaska Reddy" date="1308651284"]@sigrid, thanks for your reply. But did not get your answer. The question was is on how to assign id to a dynamically created object.[/quote]

    That is because you asked the wrong question. You asked how to assign an id. As you say yourself earlier on, that is not possible:
    [quote author="franziss" date="1308625139"]I understand that we cannot dynamically assigned id to the dynamically created object.[/quote]

    So, asking that, is obviously the wrong question. What you really wanted to know, is given that you can not use ids, what kind of mechanism you can use to refer to dynamically created objects. That is the question that sigrid answered for you.


Log in to reply
 

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