QML ListModel: The move() function



  • Hallo there,
    I have a little problem with ListModel and ListView, in particular concerning the move function of ListModel. I will show you code snippets from the DynamicList example included in the SDK with some modifications just to debug it:
    Code:

    @ Item {
    property alias nameText: nameTextBox.text
    id: delegateItem
    width: listView.width; height: 55
    clip: true

            Row {
                anchors.verticalCenter: parent.verticalCenter
                spacing: 10
    
                Column {
                    Image {
                        source: "content/pics/arrow-up.png"  
                        MouseArea {
                            anchors.fill: parent;
    
                            onClicked: {
    
                                var voice
                                var nameInModel
                                var nameInView
    
                                voice = listView.contentItem.children[1]
                                nameInModel = fruitModel.get(1).name
    
                                nameInView = voice.nameText
    
                                console.log("name before move in model at 1: "+nameInModel)
                                console.log("name before move in list at 1: "+nameInView)
    
                                 fruitModel.move(index, index-1, 1)
    
                                voice = listView.contentItem.children[1]
    
                                nameInView = voice.nameText
    
                                nameInModel = fruitModel.get(1).name
                                console.log("name after move in model at 1: "+nameInModel)
                                console.log("name after move in list at 1: "+nameInView)
    
                            }
                        }
                    }
    

    ....@

    So, when the image "content/pics/arrow-up.png" is clicked, one item in the model is moved up by one position, now, in the example there are four items in order: "Apple"- "Banana" - "Cumqat" - "Durian" and this is the result of consol.log if I click "up" on the "Banana" moving that item to the first position:

    name before move in model at 1: Banana
    name before move in list at 1: Apple
    name after move in model at 1: Apple
    name after move in list at 1: Apple

    This is what happens if I click on Apple (that now is in the 2nd place) to move it to the 1st position:

    name before move in model at 1: Apple
    name before move in list at 1: Apple
    name after move in model at 1: Banana
    name after move in list at 1: Apple

    So, first of all it is evident that the first index of ListModel is 0 while the first index of ListView is 1, then it is evident that items in the model have been moved and their index has changed but items in the list have not.
    Now, this is my problem: Imagine that "name" is the text of a TextEdit, the ListView delegate includes a TextEdit and it can be edited by the user. If I want to take that text I know that I can do this:
    Code:
    @
    for (var i = 0; i <= myListView.count; i++){
    myItem= myListView.contentItem.children[i]
    myName = myListView.name
    }@

    But here is the problem, in my ListView there is also the possibility to move the item by using the move function of ListModel but if I move items and I use the previous loop to take the "name" nothing seems to change (exactly as in the DynamicList example).
    On the other hand, if I take the "name" by using:
    @myName= myListModel.get(i).name@
    then, nothing seems to change when I modify the text in the TextEdit and I try to take the "name",

    So, what I have to do to obtain a ListView with TextEdit and with the possibility to move itemsof which I can record any changment?

    I have I have been clear, Thank you very much,
    Giammarco


Log in to reply
 

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