user1592280
user1592280

Reputation: 71

How to access item in a ListDelegates / ListView outside of it?

I am new to Qt / QML coding and I am facing one problem with respect to accessing the elements in a listdelegate in a listview.

For Example if my Qml looks like this

Item
{
 id: item_id
 property int focus_id: 0

 function setFocusImageSource() {}

 ListView
 {
  id: listView_Id
  delegate: listViewdelegate
  model: listModeldata
 }

 Component
 {
  id: listViewdelegate
  Rectangle
  {
   id: rectangle_id
   Image
   {
    id: focus_Image
    source: x.x
   }
  }
 }

 ListModel
 {
   id: listModeldata
   /*elements*/
 } 
} 

Now the basic functionality of the list view is working fine with my code ( not the above one ) how ever when I do specific operation I need to change the focusing Image . I want to change it using the function "setFocusImageSource()" . I have tried setting the image source directly using focus_Image.source = "xx" .

Is it like the Image inside the Rectangle component is local to the delegate and cannot be accessed from ITEM tag. If so How can I set the image from the function mention above.

Thanks in advance.

Chand.M

Upvotes: 4

Views: 2492

Answers (1)

Pavel Osipov
Pavel Osipov

Reputation: 2097

A counterpart of QML Component in C++ is a class. As you know you can change members' values only in class' instances - objects. The same is true for Components too: you can not change anything in Component - only in its instances. There are two possibilities to solve you problem:

  1. Bind properties of listViewdelegate to some property outside of it: property of item_id or listView_Id or something else.
  2. Bind properties of listViewdelegate to some property of the element of listModeldata.

Examples:

Image {
    id: focus_Image
    source: x.x // defualt value
    Connections {
        target: item_id
        onFocus_idChanged: {
            if ( /* some logic if needed */ ) {
                focus_Image.source = xx;
            }
        }
    }
}

or

Image {
    id: focus_Image
    source: {
        // inidicator is a property of the element of listModeldata
        if (indicator) { 
            return xx;
        }
    }
}

Upvotes: 4

Related Questions