[Solved] Strange behavior of Qml ListView



  • I have working with a qml listview that is filled from an XmlListModel.
    Now I'm trying to change the index when the view gets completed, but it's reacting in a strange way.
    When I call:
    @Component.onCompleted: listView.positionViewAtIndex(10, ListView.Contain)@
    The debugger says that the index is changed but it actually doesn't and the index always remain 1. Here is the full code.
    @import QtQuick 1.1
    import com.nokia.symbian 1.1
    Page {
    id: page
    XmlListModel{
    id: model
    source: "http://www.ilgazzettino.it/rss/home.xml"
    query: "/rss/channel/item"
    XmlRole { name: "title"; query: "title/string()" }
    XmlRole { name: "link"; query: "link/string()" }
    XmlRole { name: "description"; query: "description/string()" }
    }
    ListView{
    id: listView
    onCurrentIndexChanged: console.debug("index is changed")
    width: page.width
    height: page.height
    model: model
    spacing: 10
    interactive: true
    snapMode: ListView.SnapOneItem
    orientation: ListView.Horizontal
    delegate: Rectangle{
    color: "white"
    width: page.width
    height: page.height
    Flickable{
    id: flickable
    width: page.width
    height: page.height
    flickableDirection: Flickable.VerticalFlick
    contentWidth: descriptionText.width
    contentHeight: descriptionText.height
    Text{
    id: titleText
    text: title
    anchors{
    left: parent.left
    leftMargin: 5
    right: parent.right
    rightMargin: 5
    top: parent.top
    }
    font.bold: true
    font.pixelSize: 22
    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
    }
    Text{
    id: descriptionText
    text: description
    color: "black"
    anchors{
    left: parent.left
    leftMargin: 5
    right: parent.right
    rightMargin: 5
    top: titleText.bottom
    topMargin: 5
    }
    wrapMode: Text.WrapAtWordBoundaryOrAnywhere
    }
    }
    }
    }
    Component.onCompleted: {
    listView.positionViewAtIndex(15, ListView.Beginning)
    }
    }@
    Is there anyway to fix it?



  • Hi,

    I'm a bit unsure of what your question actually is. What behaviour are you expecting and what behaviour are you observing? Is this something to do with focus / currentIndex, or something else? Note that if you initialize currentIndex to -1 (via currentIndex:-1) the last-added item will not steal focus.

    Cheers,
    Chris.



  • The problem is that when I call:
    @Component.onCompleted: {
    listView.positionViewAtIndex(15, ListView.Beginning)
    }@
    The view doesn't change the currentIndex.
    But if I do that in this way, everything works:
    @Timer{
    id: timer
    running: true
    interval: 1000
    onTriggered: listView.positionViewAtIndex(15, ListView.Beginning)
    }@



  • My guess is that your View is completed before the model has fetched any data. The XMLListModel is asynchronous so when you position the view at row 15, there might not be any rows to position at yet. You might instead want to move the positionViewAtIndex to:

    XmlListModel { onStatusChanged: if (status == XmlListModel.Ready ) ... }



  • [quote author="Jens" date="1361264701"]My guess is that your View is completed before the model has fetched any data. The XMLListModel is asynchronous so when you position the view at row 15, there might not be any rows to position at yet. You might instead want to move the positionViewAtIndex to:

    XmlListModel { onStatusChanged: if (status == XmlListModel.Ready ) ... }[/quote]

    Thank you very much, that was the problem ( I didn't know.XmlListModel is asynchronous).
    So everything works when I call:
    @onStatusChanged: if(status==XmlListModel.Ready)
    {
    listView.positionViewAtIndex(15, ListView.Beginning)
    }@


Log in to reply
 

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