C++: how to call QML ListView selected item object?

  • My QML file has a listview which use VisualItemModel to display each item (each item is a qml page). In my C++, how to get the selected object? In another word, how to get the selected item in C++?

    My list model:

    @VisualItemModel {
    property int itemwidth: 0
    property int itemheight: 0
    page1 {
    id: p1
    width: itemwidth
    height: itemheight
    page2 {
    id: 2
    width: itemwidth
    height: itemheight
    page3 {
    id: p3
    width: itemwidth
    height: itemheight
    In the C++ code:

    @QObject rootObject = dynamic_cast<QObject>(viewer.rootObject());
    QObject lv = rootObject->findChild<QObject>("objectMyListView");@
    If I selected p1, then how to get p1 object? The page1 / page2 / page3 are 3 qml files in the same directory.

  • lv->property("currentItem");

    But normally you want QML to be calling things from C++, not the other way around. Perhaps you could expose the C++ functionality as a function, and call that function from QML with the currently selected item as an argument?

Log in to reply

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