Qml Get the text of listElement using the mouseAera

107 views Asked by At

I want to get the name of listElement (append dynamicaly with python) when I click for reuse this name with python function. How can I get this name ?

In this example I can only get the index 0 element...

QML part :

    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]});
                }
            });
        });
    }

The python part work (only print element name)

1

There are 1 answers

0
eyllanesc On BEST ANSWER

You have to set the MouseArea on the delegate:

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
}