Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

How to append function object to an ListElement and callback when the element clicked

$
0
0
Hmm, you can try something like this: import QtQuick 2.2 import QtQuick.Controls 1.2   ApplicationWindow {     visible: true     width: 480     height: 640       Rectangle {         anchors.fill: parent         color: "white"     }       Cmlist {         id: lst         anchors.fill: parent           Component.onCompleted: {             addItem(lst, "abc", "callback1")             addItem(lst, "abc", "callback2")         }           function callback1()         {             console.debug("callback1")         }         function callback2()         {             console.debug("callback2")         }     } } import QtQuick 2.0   Item {     id: lst     anchors.fill: parent       ListModel {         id: lstmodel     }       Component {         id: lstdelegate           Rectangle {             id: lstitem             width: lst.width             height: 80             color: "steelblue"               Rectangle {                 anchors.fill: parent                 color: "#7A7A7A"                 visible: mouse.pressed             }               Text {                 id: lsttext                 text: str                 anchors.verticalCenter: parent.verticalCenter                 anchors.left: parent.left                 anchors.leftMargin: 10                 font.pixelSize: parent.height * 0.5                 font.bold: true                 color: "black"             }               Rectangle {                 height: 1                 width: parent.width                 color: "lightgray"                 anchors.bottom: parent.bottom             }               signal clicked             onClicked: {                 cmp.call(callback)             }               MouseArea {                 id: mouse                 anchors.left: parent.left                 anchors.right: parent.right                 anchors.top: parent.top                 anchors.bottom: parent.bottom                 onClicked: {                     lstitem.clicked()                 }             }         }     }       ListView {         id: lstview         anchors.fill: parent         model: lstmodel         delegate: lstdelegate     }       function addItem(cmp, str_, callback_)     {         lstmodel.append({             "cmp": cmp             "str": str_,             "callback": callback_         })     } } You see the changes? The trick is to pass the object where the functions are defined (scope) to the model and use call with a string (the name of the function). You can also pass parameters. Everything after the function name will pass as parameter like: cmp.call(callback, param1, param2,…) I’m not at home so no guaranty :)

Viewing all articles
Browse latest Browse all 4972

Trending Articles