Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Strange behavior of Qml ListView

[Solved] Strange behavior of Qml ListView

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daljit97
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daljit97
        wrote on last edited by
        #3

        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)
        }@

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jens
          wrote on last edited by
          #4

          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 ) ... }

          1 Reply Last reply
          0
          • D Offline
            D Offline
            daljit97
            wrote on last edited by
            #5

            [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)
            }@

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved