PDA

View Full Version : Different actions on ListElements



Icamer
1st May 2016, 16:55
Hi. I want to make a working MouseArea on ListElements. If I just add MouseArea to an Item, then every ListElement would do the same thing. How do i make a working menu of these two (I mean making them do different things on click). Let's say Name1 should change it's name onclick, and Name2 should close app.

ListView {
id: listView2
x: 20
y: 430
orientation: ListView.Horizontal
anchors.rightMargin: 20
anchors.leftMargin: 20
anchors.bottomMargin: 10
anchors.top: listView1.bottom
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.topMargin: 50
model: ListModel {

ListElement {
name: "Name1"
sourcepath: "icon1.png"
}
ListElement {
name: "Name2"
sourcepath: "icon2.png"
}
}
delegate: Item {
x: 5
width: 150
height: 40
Row {
id: row2
spacing: 0
Image {
width: 40
height: 40
source: sourcepath
}
Text {
text: name
font.bold: true
anchors.verticalCenter: parent.verticalCenter
}
}
}
}

Also how do I add smth like a button, that would add listelement with choosen values.

anda_skoa
1st May 2016, 21:34
If you put a MouseArea into your delegate, then each item will have its own mouse area.

You can either use a model value or the model's index to decide what you want to do in each of them.

Cheers,
_