Qml 使用 mouseAera 获取 listElement 的文本

Qml Get the text of listElement using the mouseAera

我想在单击以使用 python 函数重用此名称时获取 listElement 的名称(使用 python 动态附加)。我怎样才能得到这个名字?

在此示例中,我只能获取索引 0 元素...

QML 部分:

    Rectangle {
    id: listRowDb
    anchors.top: toolbar.bottom  
    width: head_row.width   
    height: units.gu(100)
    ListModel {
        id: listDb

        ListElement {   
            name: ""
        }
    }

    Component {
        id: listDbDelegate
        Row {
            spacing: 100
            Text { text: 'Nom de la DB : ' + name}
        }
    }
    ListView {
        id: listView1
        anchors.fill: parent
        model: listDb
        delegate: listDbDelegate
        #Don't work. When I click I get the index 0 name.
        MouseArea {
                anchors.fill: parent
                onClicked: {python.call('example.speak2', [listDb.get(listView1.currentIndex).name], function(returnValue) {console.log(returnValue)});}
        }
    }
}
Python {
    id: python

    Component.onCompleted: {
        addImportPath(Qt.resolvedUrl('../src/'));

        importModule('example', function() {
            console.log('module imported');
            #Just a litlle test
            python.call('example.speak', ['Hello World!'], function(returnValue) {
                console.log('example.speak returned ' + returnValue);
            })
            #Python list all DB and append in listModel
            python.call('example.listDb', [], function(returnValue) {
                for(var i=0; i<returnValue.length; i++) {
                    listDb.append({"name":returnValue[i]});
                }
            });
        });
    }

python部分工作(仅打印元素名称)

您必须在委托上设置 MouseArea:

Component {
    id: listDbDelegate
    Row {
        spacing: 100
        Text {
            text: 'Nom de la DB : ' + name
            MouseArea{
                anchors.fill: parent
                onClicked: console.log(name, index)
            }
        }
    }
}
ListView {
    id: listView1
    anchors.fill: parent
    model: listDb
    delegate: listDbDelegate
}