How to access to a listview element



  • I need to get a variable status from each items in a listview.
    My function is

                for (var count= 0; count< listViewModel.count; count++) {
                    console.log(count, listView.contentItem.children[count])
                }
    

    The output is

    qml: 0 QQBlueBus_QMLTYPE_109(0x556e40f212a0)
    qml: 1 QQBlueBus_QMLTYPE_109(0x556e40f0c4a0)
    qml: 2 QQBlueBus_QMLTYPE_109(0x556e40f1e590)
    qml: 3 QQBlueBus_QMLTYPE_109(0x556e40f349a0)
    qml: 4 QQBlueBus_QMLTYPE_109(0x556e40f63f60)
    qml: 5 QQBlueBus_QMLTYPE_109(0x556e40f5a720)
    qml: 6 QQBlueBus_QMLTYPE_109(0x556e40f6dc30)
    qml: 7 QQBlueBus_QMLTYPE_109(0x556e40f80c20)
    qml: 8 QQBlueBus_QMLTYPE_109(0x556e40f93c10)
    qml: 9 QQBlueBus_QMLTYPE_109(0x556e40fb4f50)
    qml: 10 QQBlueBus_QMLTYPE_109(0x556e40fc8350)
    qml: 11 QQBlueBus_QMLTYPE_109(0x556e40ff8380)
    qml: 12 QQBlueBus_QMLTYPE_109(0x556e40feee50)
    qml: 13 QQBlueBus_QMLTYPE_109(0x556e4101e390)
    qml: 14 QQBlueBus_QMLTYPE_109(0x556e41014f90)
    qml: 15 QQuickItem(0x556e41050610)
    qml: 16 undefined
    qml: 17 undefined
    qml: 18 undefined
    qml: 19 undefined
    qml: 20 undefined
    qml: 21 undefined
    qml: 22 undefined
    qml: 23 undefined
    qml: 24 undefined
    qml: 25 undefined
    qml: 26 undefined
    qml: 27 undefined
    qml: 28 undefined
    qml: 29 undefined
    qml: 30 undefined
    qml: 31 undefined
    qml: 32 undefined
    qml: 33 undefined
    qml: 34 undefined
    qml: 35 undefined
    qml: 36 undefined
    qml: 37 undefined
    qml: 38 undefined
    qml: 39 undefined
    qml: 40 undefined
    qml: 41 undefined
    qml: 42 undefined
    qml: 43 undefined
    qml: 44 undefined
    qml: 45 undefined
    qml: 46 undefined
    qml: 47 undefined
    qml: 48 undefined
    qml: 49 undefined
    qml: 50 undefined
    qml: 51 undefined
    qml: 52 undefined
    qml: 53 undefined
    qml: 54 undefined
    qml: 55 undefined
    qml: 56 undefined
    qml: 57 undefined
    qml: 58 undefined
    qml: 59 undefined
    qml: 60 undefined
    qml: 61 undefined
    qml: 62 undefined
    qml: 63 undefined
    qml: 64 undefined
    qml: 65 undefined
    qml: 66 undefined
    qml: 67 undefined
    qml: 68 undefined
    qml: 69 undefined
    qml: 70 undefined
    qml: 71 undefined
    qml: 72 undefined
    qml: 73 undefined
    qml: 74 undefined
    qml: 75 undefined
    qml: 76 undefined
    qml: 77 undefined
    qml: 78 undefined
    qml: 79 undefined
    qml: 80 undefined
    qml: 81 undefined
    qml: 82 undefined
    

    but it is happening something strange.
    I can access at only 15 elements on my listview the others are undefined.
    Where is the error?



  • @mrdebug said in How to access to a listview element:

    listViewModel

    what's in listViewModel
    what model are you using
    show us how you are creating/getting the data - have you proved what's in the container differs from your log? The locals and watches window should let you step through that.

    Have you read? http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#qml-data-models

    That page has the following headings, and further links. Here's a quick summary but I suggest you expose yourself to that page above if you haven't already.

    List Model http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html
    XML Model http://doc.qt.io/qt-5/qml-qtquick-xmllistmodel-xmllistmodel.html
    Object Model http://doc.qt.io/qt-5/qtquick-views-example.html#objectmodel
    Integers as Models ie:

    ListView {
           anchors.fill: parent
           model: 5
           delegate: itemDelegate
       }
    

    Object Instances as Models (
    C++ Data Models http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html



  • This is my code:

                    ScrollView {
                        id: scrollView1
                        anchors.fill: parent
                        ListView {
                            id: listView;
                            anchors.rightMargin: 1
                            anchors.leftMargin: 1
                            anchors.left: parent.left
                            anchors.bottom: parent.bottom
                            anchors.top: parent.top
                            spacing: 1
                            model: listViewModel;
                            delegate: listViewDelegate;
                        }
                    }
    
                    Component {
                        id: listViewDelegate;
                        QQAdc {
                            width: parent.width
                        }
                    }
    
                    ListModel {
                        id: listViewModel;
                    }
    

    to add element

    listViewModel.append({sDescription: sDescription, sValue1: sValue1})
    

    Now I need to get a property from each element.



  • Hi @mrdebug :)

    try this : listViewModel.get(indexYouNeed).sValue1


Log in to reply
 

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