Accessing Delegates from Outside the Delegate



  • Hello,

    Can we access delegates(ListView) from outside a Delegate Declaration.Say i want to change a property like state of a Delegate.
    For eg. in this sample code can i access a particular row delegate of list_view1 ?

    @
    Component{
    id:listDelegate

        Item{
    
         }
    

    }

    ListView {
    id: list_view1
    width:parent.width
    height: parent.height

            model: myModel
            delegate:listDelegate
    
        }
    

    @



  • This is not possible in general since delegates are created on-demand (and thrown away at will when no longer necessary). So the delegate you are requesting may in fact not exist.

    So you have to either expose the necessary properties to allow the delegate to react to that, or the delegate can call a method and pass itself as a parameter, then you know you can access the delegate through this parameter.



  • Yes,Delegates are actually deleted but we can store pointers to them in javascript(I know its not advisable , but it helps achieve some fancy UI effects).



  • I have a question about this solution:

    Suppose the delegate calls a method that put it on a variable (say myDelegate). This variable can be someone else's property or just a javascript variable.

    Now, suppose I remove, from the model, the item associated with this delegate instance. Will the instance itself be deleted? Or will it be retained, since there's still a reference to it in myDelegate variable? In other words, the value in myDelegate will then be undefined?



  • yes the value in myDelegate will be undefined.A solution to this problem is to reset the variable in myDelegate to NULL whenever the list starts moving( onMovementStarted: ) just to make sure it doesn't point to an invalid object/delegate


Log in to reply
 

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