Section text issue in ListView



  • Hello,

    I wrote a little example-program, which simulates my problem:

    import QtQuick 2.0
    import QtQuick.Controls 2.0
    
    Rectangle {
    	id: root
        width: 300
        height: 290
        color: "#00FFFF"
    
        ListView {
            anchors.fill: parent
            anchors.margins: 20
    
            clip: true
    
            model: spaceMen
    
            delegate: spaceManDelegate
    
            section.property: "sec"
            section.delegate: sectionDelegate
        }
    
        Component {
            id: spaceManDelegate
    
            Item {
                width: ListView.view.width
                height: 20
                Text {
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.leftMargin: 8
                    font.pixelSize: 12
                    text: name
                    color: '#1f1f1f'
                }
            }
        }
    	property int added: 0
        Component {
            id: sectionDelegate
    
            Rectangle {
    			color: "#157efb"
                width: ListView.view.width
                height: 20
                Label {
    				anchors.fill: parent
    				horizontalAlignment: Text.AlignHCenter
    				verticalAlignment: Text.AlignVCenter
                	text: section
                	color: '#e0e0e0'
    			}
                
                MouseArea {
    
    				anchors.fill: parent
    				
    				onClicked: {
    					root.added++
    					spaceMen.append( { name: "Added: " + root.added, sec: section } )
    					console.log(added)
    					spaceMen.sort()
    				}
    			}
            }
        }
    
    
        ListModel {
            id: spaceMen
    
            function sort() {
    			for(var i = 0; i < count; ++i) {
    				for(var j = 0; j < count; ++j) {
    					if(get(i).sec === get(j).sec)
    						move(j, i, 1)
    				}
    			}
    		}
    
            ListElement { name: "1"; sec: "0001"; }
            ListElement { name: "2"; sec: "0002"; }
            ListElement { name: "3"; sec: "0001"; }
            ListElement { name: "4"; sec: "0004"; }
            ListElement { name: "5"; sec: "0001"; }
            ListElement { name: "6"; sec: "0003"; }
            ListElement { name: "7"; sec: "0001"; }
            ListElement { name: "8"; sec: "0006"; }
            ListElement { name: "9"; sec: "0005"; }
            ListElement { name: "10"; sec: "0002"; }
            ListElement { name: "11"; sec: "0002"; }
            ListElement { name: "12"; sec: "0001"; }
            ListElement { name: "13"; sec: "0004"; }
            ListElement { name: "14"; sec: "0001"; }
            ListElement { name: "15"; sec: "0005"; }
            ListElement { name: "16"; sec: "0001"; }
            ListElement { name: "17"; sec: "0006"; }
            ListElement { name: "18"; sec: "0001"; }
            ListElement { name: "19"; sec: "0002"; }
            ListElement { name: "20"; sec: "0001"; }
            ListElement { name: "21"; sec: "0007"; }
            ListElement { name: "22"; sec: "0001"; }
            
            
            Component.onCompleted: sort()
        }
    }
    
    

    The problem is, that after adding new elements to a ListModel the section text of another (it seems to be random) section is changed to the text of the section, in which it has been added. You have to click simply often on the different sections. Here is a little screenshot:
    0_1500408794070_c7b0c713-f94d-41a4-ba14-cdb6c0836f3f-grafik.png
    The elements of section 0002 are correct, only the section text is wrong.

    I hope you understand what I mean (it is not easy to describe) and I am sorry for any English mistakes.

    Thank you!


Log in to reply
 

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