[Solved] set (access,highlight) ListView currentItem/currentIndex from C++



  • hi:)
    I would like to highlight the currentItem of the ListView from C++ code according to C++ events, in particular a USB controller input (never mind). I have read many forum posts but did not find a suitable solution. Could you help me?

    My list is simple for the start:
    @
    ListView {
    focus: true
    id: categories
    anchors.fill: parent
    model: myModel
    delegate:
    Row {
    id: foo
    width: foo.ListView.view.width; height: 60
    Text {
    id: t
    text: model.display //I set the model in c++ with setContextProperty
    color: foo.ListView.isCurrentItem ? "white" : "black"
    font { family: "Helvetica"; pixelSize: 16; bold: true }
    anchors {
    left: parent.left; leftMargin: 15
    verticalCenter: parent.verticalCenter
    }
    MouseArea{
    anchors.fill: parent
    }
    }

            MouseArea {
                anchors.fill: foo
                onClicked: {
                    foo.ListView.view.currentIndex = index
                    myModel.doSth(t.text)  //works
                }
            }
        }
    
        highlight: Rectangle { color: "green" }
        highlightMoveSpeed: 500
    }
    

    @

    cheers
    simon:)



  • You can use Connections QML item to connect to signals from C++ object (that you can set via setContextProperty as you've set model object).



  • Somethink like:

    @Connections {
    target: myModel
    onChangeCurrentIndex: { ... }
    }@

    Of course, your model in C++ should have changeCurrentIndex signal.



  • ...I should actually be banned and cast out for such question for not beeing able to look in help accordingly, shouldn't I? ...I still hope i won't be and I am happy that there are people like you patiently answering such questions...

    Of course:
    @
    Connections {
    target: myModel
    onChangeCurrentIndex: {
    console.log("yo")
    categories.currentIndex = idx
    }
    }
    @

    in c++ h/cpp
    @
    signals:
    void changeCurrentIndex(int idx);

    ...

    emit changeCurrentIndex(1);
    @

    this does the job.

    thakns+cheers
    simon:)


Log in to reply