Access function/slot loader element in listview



  • Hello,

    I would like to know if it's possible to call a function in component loaded in listview.
    Let me explain :

    I have a main.qml file with a Lisview to display my application pages :

    //main.qml
    ListView {
            id:myListView
            anchors.fill: parent
            orientation: ListView.Horizontal
            cacheBuffer: main.width * (NbPages-1) 
            model: PageNames
            delegate: Loader {
                id: myLoader
                source: modelData + ".qml"
            }
        }
    

    And in every item of "modelData " I have a function called "cyclicActions"

    //page1.qml
    function cyclicActions()
    {
       ----
    }
    
    //page2.qml
    function cyclicActions()
    {
       ----
    }
    

    So I want to call cyclicActions only for the currentItem of the Listview.
    I saw ListView.currentItem method but I can't access to the function.

    Can you help me with that ?

    Than you


  • Moderators

    @Delta_sim said in Access function/slot loader element in listview:

    I saw ListView.currentItem method but I can't access to the function.

    How did you try to access it?

    In general, something like this might work:

    // Some JS block:
    myListView.currentItem.item.cyclicActions()
    

  • Moderators

    @Delta_sim said in Access function/slot loader element in listview:

    I saw ListView.currentItem method but I can't access to the function.

    as @sierdzio said.
    If you call your method on listView.currentItem directly you are actually calling it on the Loader item



  • Indeed it works !
    I did 2 mistakes, I forgot ".item" after currentItem and I didn't changed currentIndex when I switch between pages because I use positionViewAtIndex. But positionViewAtIndex don't change currentIndex of the listView.

    Thanks guys !