Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ListView.onAdd not triggered with loader as delegate
Forum Updated to NodeBB v4.3 + New Features

ListView.onAdd not triggered with loader as delegate

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 866 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Gir
    wrote on last edited by
    #1

    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
            }
          }
        }
      }
    }
    
    raven-worxR 1 Reply Last reply
    0
    • J Jim Gir

      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
              }
            }
          }
        }
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jim Gir
        wrote on last edited by
        #3

        @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

        raven-worxR 1 Reply Last reply
        0
        • J Jim Gir

          @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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jim Gir
            wrote on last edited by Jim Gir
            #5

            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

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jim Gir
              wrote on last edited by
              #6

              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.

              raven-worxR 1 Reply Last reply
              0
              • J Jim Gir

                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.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #7

                @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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved