Find the *current* number of delegate in the ListView



  • According to the doc, ListView.count returns the number of items in the view (not the number of items in the model). My code (myListV.count) displayed the number of items in the model even though only three delegates had been created.

    create delegate: 0, visible items: 9
    create delegate: 1, visible items: 9
    create delegate: 2, visible items: 9

    Thanks

    @// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1

    Rectangle {
    width: 300
    height: 200
    color: "green"
    ListModel {
    id: myListModel
    objectName: "myListModel"
    ListElement {
    name: "Bill Smith"
    number: "555 3264"
    }
    ListElement {
    name: "John Brown"
    number: "555 8426"
    }
    ListElement {
    name: "Sam Wise"
    number: "555 0473"
    }
    ListElement {
    name: "A"
    number: "555 3264"
    }
    ListElement {
    name: "B"
    number: "555 8426"
    }
    ListElement {
    name: "C"
    number: "555 0473"
    }
    ListElement {
    name: "D"
    number: "555 3264"
    }
    ListElement {
    name: "E"
    number: "555 8426"
    }
    ListElement {
    name: "F"
    number: "555 0473"
    }
    }

    ListView {
        id: myListV
        objectName: "myListV"
        anchors.fill: parent;
    
        model: myListModel
        delegate: Rectangle{
            objectName: "myDelegate"
            width: parent.width;height: myListV.height / 3
            border.color: "black"
            color: "yellow";
            Text {
                id: myText
                text: name + ": " + number
                color: "red"
            }
            Component.onCompleted: console.log("create delegate: " +index + ", visible items: " + myListV.count)
            Component.onDestruction: console.log("on destruction: " + index)
        }
    
        Component.onCompleted: { console.log("objectName: " + objectName)}
    }
    

    }@



  • What is exactly your question?

    You want to find how many delegates are visible at a given time?

    For your information, the number of visible list items is not necessarily equal to the number of the live delegate instances (created but not destroyed yet).



  • The number of live delegates should be equal the number of invocations on delegate's Component.onCompleted, right? I intentionally made only 3 visible items at a time. As I scrolled down, the new item was created and old item was destroyed, but the number of ListView.count remained the same.

    create delegate: 0, visible items: 9
    create delegate: 1, visible items: 9
    create delegate: 2, visible items: 9
    objectName: myListV
    create delegate: 3, visible items: 9
    create delegate: 4, visible items: 9
    create delegate: 5, visible items: 9
    on destruction: 1
    on destruction: 2
    create delegate: 6, visible items: 9
    create delegate: 7, visible items: 9
    on destruction: 3
    create delegate: 8, visible items: 9
    on destruction: 4
    on destruction: 5
    on destruction: 6
    create delegate: 6, visible items: 9


    I would like to know at a given time, how many live delegates exist in memory, how to iterate through those delegates to inspect their values. The issue I have is that one for a while the ListView displayed a blank delegate.

    Thanks



  • To find the number of live delegates, I create a list, add index to the list in delegate.Component.onCompleted, and remove index from the list in delegate.Component.onDestruction. However, I still haven't found the reason my ListView displayed correctly and one for a while drew a blank delegate.



  • An easy way to see the number of the delegates that are alive at a given time is the following:

    @
    import QtQuick 1.1

    Rectangle {
    width: 300
    height: 200
    color: "green"

    property int numberOfVisibleItems:0
    
    ListModel {
        id: myListModel
        objectName: "myListModel"
        ListElement {
            name: "Bill Smith"
            number: "555 3264"
        }
        ListElement {
            name: "John Brown"
            number: "555 8426"
        }
        ListElement {
            name: "Sam Wise"
            number: "555 0473"
        }
        ListElement {
            name: "A"
            number: "555 3264"
        }
        ListElement {
            name: "B"
            number: "555 8426"
        }
        ListElement {
            name: "C"
            number: "555 0473"
        }
        ListElement {
            name: "D"
            number: "555 3264"
        }
        ListElement {
            name: "E"
            number: "555 8426"
        }
        ListElement {
            name: "F"
            number: "555 0473"
        }
    }
    
    ListView {
        id: myListV
        objectName: "myListV"
        anchors.fill: parent;
    
        model: myListModel
        delegate: Rectangle{
            objectName: "myDelegate"
            width: parent.width;height: myListV.height / 3
            border.color: "black"
            color: "yellow";
            Text {
                id: myText
                text: name + ": " + number
                color: "red"
            }
            Component.onCompleted: numberOfVisibleItems++;
            Component.onDestruction: numberOfVisibleItems--;
        }
    }
    
    onNumberOfVisibleItemsChanged: console.log("Visible items: " + numberOfVisibleItems)
    

    }

    @

    Regarding your specific problem with the blank delegate i have no idea at the moment.


Log in to reply
 

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