Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Can't set ListView.currentIndex

    QML and Qt Quick
    2
    2
    2278
    Loading More Posts
    • 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.
    • K
      keolsen last edited by

      Hi

      As the title says, I am not able to set currentIndex of the ListView...
      I have the ListView defined in one file and the model and delegate in seperate files.
      Everything seems to work fine except that I'm not able to set the currentIndex from the delegate.
      It's not unlikely I'm doing something totally wrong...I'm still not super sharp in this.
      Below is an example of what I'm trying to achieve...

      Thanks in advance.

      Example:

      myListView.qml

      @ListView {
      model: myModel
      delegate: myDelegate {}
      }
      @

      myDelegate.qml

      @Component {
      // something
      MouseArea {
      onClicked {
      ListView.currentIndex = index;
      }
      }
      }@

      1 Reply Last reply Reply Quote 0
      • F
        favoritas37 last edited by

        First of all the problem is at the line:
        @
        ListView.currentIndex = index;
        @

        You reference the property currentIndex through the type name of the component (ListView), not the id of the instance of the component as it should be.

        You can either try to just write the following and see what happens:
        @
        currentIndex = index;
        @

        Or in worst case scenario you can do something like the following:

        myListView.qml
        @
        ListView {
        model: myModel
        delegate: myDelegate {
        onClicked: currentIndex = index;
        }
        }
        @

        myDelegate.qml

        @
        Component {
        // something
        signal clicked(int index)

          MouseArea {
            onClicked {
                parent.clicked(index)
            }
          }
        }
        

        @

        1 Reply Last reply Reply Quote 0
        • First post
          Last post