[Solved] QML ListView delegate problem



  • I have a strange problem with ListView when I iterate over visible items in the view. I`m using this code to go over all the delegates that are instantiated currently in the view:

    @for (var i = 0; i < messagelist.contentItem.children.length; i++)
    {
    var curItem = messagelist.contentItem.children[i];
    console.log(i,": ", curItem, "=", curItem.messagetext);
    }@

    First time when I fill the model with data I get in the console output number of items visible +1. The first one is different type from the others:

    bq. 0 : QDeclarativeItem(0xf5f500) = undefined
    1 : MessageBox_QMLTYPE_5(0xbb63a0, "messagebox") = rewgrew
    2 : MessageBox_QMLTYPE_5(0x14b1130, "messagebox") = g
    3 : MessageBox_QMLTYPE_5(0x1333b30, "messagebox") = f
    4 : MessageBox_QMLTYPE_5(0x1327e50, "messagebox") = f
    5 : MessageBox_QMLTYPE_5(0x10169c0, "messagebox") = h
    6 : MessageBox_QMLTYPE_5(0x13061e0, "messagebox") = f
    7 : MessageBox_QMLTYPE_5(0x13585a0, "messagebox") = d
    8 : MessageBox_QMLTYPE_5(0x1304f90, "messagebox") = f
    9 : MessageBox_QMLTYPE_5(0x1500390, "messagebox") = h
    10 : MessageBox_QMLTYPE_5(0x1116a50, "messagebox") = h
    11 : MessageBox_QMLTYPE_5(0x1529f10, "messagebox") = f
    12 : MessageBox_QMLTYPE_5(0x1164f90, "messagebox") = s
    13 : MessageBox_QMLTYPE_5(0x132f090, "messagebox") = a
    14 : MessageBox_QMLTYPE_5(0x1540070, "messagebox") = n
    15 : MessageBox_QMLTYPE_5(0x14e04e0, "messagebox") = pppp
    16 : MessageBox_QMLTYPE_5(0x1180bb0, "messagebox") = s
    17 : MessageBox_QMLTYPE_5(0x1242250, "messagebox") = a
    18 : MessageBox_QMLTYPE_5(0x1222830, "messagebox") = vvvvvvv
    19 : MessageBox_QMLTYPE_5(0x100cbb0, "messagebox") = ggr
    20 : MessageBox_QMLTYPE_5(0x13c8050, "messagebox") = grs
    21 : MessageBox_QMLTYPE_5(0x14b0610, "messagebox") = cds
    22 : MessageBox_QMLTYPE_5(0x149ce90, "messagebox") = ttt

    If I clear the model and populate it with same data again that item disappears. I have tried with the model from C++, but also with some dummy model declared in QML with ListModel. Same thing happens. Cachebuffer is set to 0 by default.

    Here is the code from test project where I made a small model and the view for it and where I get the same results:

    @Rectangle {
    width: 360
    height: 360

    ListModel
    {
        id: settingspropmodel;
        ListElement { txt: "Mark all as read"}
        ListElement { txt: "Add people"}
        ListElement { txt: "Set topic"}
        ListElement { txt: "Set icon"}
        ListElement { txt: "Notifications off"}
        ListElement { txt: "Leave chat"}
    }
    
    ListView
    {
        id: lv;
        boundsBehavior: Flickable.DragOverBounds
        clip: true
    
        anchors.fill: parent;
    
        onMovementEnded:
        {
            for (var i = 0; i < lv.contentItem.children.length; i++)
            {
                var curItem = lv.contentItem.children[i];
                console.log(i,": ", curItem, "=", curItem.msgtxt);
            }
            console.log("--------------------------------------------");
        }
    
        model: settingspropmodel
        delegate: Item
        {
            property string msgtxt: txt;
            Rectangle { width: parent.width; height: 1; x:0; y:0; border.color: "#cccccc"; border.width: 1}
            Text { id: proba1; width: parent.width;  x: 10; y:5; color: "#424242"; font.family: "Ubuntu Light"; font.pointSize: 10; wrapMode: Text.WordWrap; text: txt; textFormat:Text.RichText;}
            anchors.right: parent.right;
            anchors.left: parent.left;
            height: proba1.height+20;
        }
    }
    

    }@



  • Is the header component automatically parented to the view, perhaps?



  • I did not specify any header component, and it is strange that the unknown item is always second. Also if this was the header than it would appear after I repopulate the model too.



  • Could be the highlight (focus) item showing up here as child of the contentItem node.



  • I will checkout if this is the case, but I`m suspicious about that.



  • I have found out what is the cause of this problem. As Roland suggested that it could be the highlight item I started thinking in that way. Because I didnt set the currentIndex to -1 it was generating some kind of highlight even if I didnt specify anything for highlight.

    Workaround for this is to set currentIndex to -1 on Component.onCompleted.


Log in to reply
 

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