Property MouseArea for each ListElement



  • Hello!
    I am a beginner user of Qt Project.

    Can't add a property for each element on ListModel.

    Code:
    @Rectangle {
    id: settings
    x: 0
    y: parent.height/10
    width: parent.width/43
    height: parent.height/10
    9
    color: "black"
    opacity: 0.5
    visible: false
    ListView {
    id: settingList
    x:0
    y:0
    width: parent.width
    height: parent.height
    model: ListModel {
    ListElement {
    name: "one"
    }
    ListElement {
    name: "two"
    }
    ListElement {
    name: "three"
    }
    ListElement {
    name: "four"
    }
    ListElement {
    name: "five"
    }
    }
    delegate: Item {
    width: settings.width
    height: 30
    Row {
    id: settingRow
    Rectangle {
    id: selectSetting
    width:settings.width
    height: 30
    color: "#00000000"
    Rectangle {
    y: 30
    width: settings.width
    height: 1
    color: "black"
    }
    Text {
    text: name
    font.bold: true
    anchors.verticalCenter: parent.verticalCenter
    }
    }
    }
    }
    }
    }@

    I want the property to be individualized for each list item.

    For example:

    Element "one"
    MouseArea {
    OnClicked: { function1}
    }

    Element "two"
    MouseArea {
    OnClicked: { function2 }
    }


  • Moderators

    Hi,

    You will have to do that in the delegate. Since delegate is an item you can provide it a MouseArea. For eg.
    @
    delegate: Item {
    MouseArea {
    anchors.fill: parent
    onClicked: console.log(index,model.name)
    }
    }
    @


Log in to reply
 

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