Can't set ListView.currentIndex



  • 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;
    }
    }
    }@



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

    @


Log in to reply
 

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