Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ListView.onAdd not triggered with loader as delegate



  • Hi,
    I have the following listview with conditional delegate loaded via a loader. Wherever I put ListView.onAdd in loader or delegate component, it's never triggered :

    import QtQuick 2.14
    import QtQuick.Controls 2.14
    
    ListView {
      id: lv
    
      Component {
        id: imageDelegate
        AnnotableImage {
          sectionId: curSectionId
          base: lv
          ListView.onAdd: {
            print(" on delegate")
          }
        }
      }
    
      delegate: Component {
        Loader {
          property int curSectionId: page.id
          ListView.onAdd: {
            print("on loader")
          }
          sourceComponent: switch (page.classtype) {
            case "ImageSection": {
              return imageDelegate
            }
          }
        }
      }
    }
    

  • Moderators

    @Jim-Gir
    when do you expect it to trigger?
    How do you add your items to the ListView's model.



  • @raven-worx , items are added vie via the insertRows method of a QAbstractListModel (wich contains begininsert and endinsert). this works good. because Item is added.
    What I want to do: is to to set the currentIndex after item is inserted. something like this :

    SomeDelegate {
        ListView.onAdd:  myListView.currentIndex = index
    } 
    

    THe idea is to scroll to new inserted item


  • Moderators

    @Jim-Gir
    in this example a animation is added to this attached signal handler...maybe this is a flaw in the docs.
    You can try to add an animation with a ScriptAction



  • Thanks it helps. @raven-worx
    I think I understand where is the problem. tell me if I'm wrong
    ListView.onAdd can only be triggered if the item is built immediately. I mean If I'm at beggining of the listview and a I insert an item a the end of the listview., if listview is big enough, onAdd will not be triggered since the item is not built.

    So for My use case I'can't use onAdd. I think I have to use onCountChanged of something like that.

    Thank you for you help



  • I'll close it since I was wrong. ListView.onAdd is triggered, even with loader, I was just wrong in how to use it.

    About scrolling to inserted items, here is my solution for the posterity:

    ListView{
    function onItemAdded(modelIndex, row, col) {
        currentIndex = row
      }
    Component.onCompleted: {
        model.rowsInserted.connect(onItemAdded)
      }
    }
    

    it does exactly what I wanted first.


  • Moderators

    @Jim-Gir
    i think the problem is rather the following:
    The ListView only creates item in the visible area and destroys the other ones when they leave the visible area.
    So you wont receive a call to the onAdd handler, because the item isn't available because it is out of the visible area anyway?
    This can be verified by inserting the items in the first position in the model rather the last one (which always will be outside of the visible area).

    So yes the solution you found is the correct approach.


Log in to reply