Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Why onCurrentIndexChanged in ListView isn't executed?
Qt 6.12 LTS is out! See what's new in the release blog

Why onCurrentIndexChanged in ListView isn't executed?

Scheduled Pinned Locked Moved QML and Qt Quick
listviewqml
2 Posts 1 Posters 1.8k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Why onCurrentIndexChanged is called only once at startup? When I change 'page' then currentIndex doesn't change. Here is my code:

        ApplicationWindow
        {
            visible: true
    
            ListView
            {
                anchors.fill: parent
    
                orientation: ListView.Horizontal
                snapMode: ListView.SnapOneItem
    
                onCurrentIndexChanged: console.log("currentIndex changed: " + currentIndex)
    
                model: 10
    
                delegate: Rectangle
                {
                    width: ListView.view.width
                    height: ListView.view.height
    
                    color: "#ffffffff";
                    border.color: "#ff000000";
    
                    Text
                    {
                        anchors.centerIn: parent
                        text: index
                    }
                }
            }
        }
    
    ? 1 Reply Last reply
    0
    • ? A Former User

      Why onCurrentIndexChanged is called only once at startup? When I change 'page' then currentIndex doesn't change. Here is my code:

          ApplicationWindow
          {
              visible: true
      
              ListView
              {
                  anchors.fill: parent
      
                  orientation: ListView.Horizontal
                  snapMode: ListView.SnapOneItem
      
                  onCurrentIndexChanged: console.log("currentIndex changed: " + currentIndex)
      
                  model: 10
      
                  delegate: Rectangle
                  {
                      width: ListView.view.width
                      height: ListView.view.height
      
                      color: "#ffffffff";
                      border.color: "#ff000000";
      
                      Text
                      {
                          anchors.centerIn: parent
                          text: index
                      }
                  }
              }
          }
      
      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Ok, solution found. currentIndex/onCurrentIndexChanged is useless in this scenario. Here is updated code:

      ApplicationWindow
      {
          visible: true
      
          ListView
          {
              anchors.fill: parent
      
              orientation: ListView.Horizontal
              snapMode: ListView.SnapOneItem
      
              model: 10
      
              onMovementEnded: console.log("current item changed: " + itemAt(contentX,contentY).itemIndex)
      
              delegate: Rectangle
              {
                  property int itemIndex: index
      
                  width: ListView.view.width
                  height: ListView.view.height
      
                  color: "#ffffffff";
                  border.color: "#ff000000";
      
                  Text
                  {
                      anchors.centerIn: parent
                      text: index
                  }
              }
          }
      }
      
      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
      • Unsolved