Pass callback function from model to view [ ListElement: cannot use script for property value]
-
wrote on 7 Feb 2023, 07:02 last edited by
How to pass function from model to view?
I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].ListModel{ id:quitModel ListElement{ title:qsTr("back") func:stackView.pop();//<---------ERROR } ListElement{ title:qsTr("quit") func:Qt.quit() } } GridView{ model:quitModel anchors.fill: parent delegate:Button{ width:cellWidth text:title onClicked:func } }
-
How to pass function from model to view?
I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].ListModel{ id:quitModel ListElement{ title:qsTr("back") func:stackView.pop();//<---------ERROR } ListElement{ title:qsTr("quit") func:Qt.quit() } } GridView{ model:quitModel anchors.fill: parent delegate:Button{ width:cellWidth text:title onClicked:func } }
May be try building the model dynamically & use it.
See something like this.ListModel { Component.onCompleted: { [ { name: "Dheeru", fun: printMe() }, { name: "PthinkS.com", fun: printAll() } ].forEach(function(element1) { append(element1); }); } }
-
How to pass function from model to view?
I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].ListModel{ id:quitModel ListElement{ title:qsTr("back") func:stackView.pop();//<---------ERROR } ListElement{ title:qsTr("quit") func:Qt.quit() } } GridView{ model:quitModel anchors.fill: parent delegate:Button{ width:cellWidth text:title onClicked:func } }
wrote on 9 Feb 2023, 20:39 last edited by fcarney 2 Sept 2023, 20:40@ynatynat I have found the ListElement mangles some objects (Actions) as well. I ended up creating a list of dictionaries instead:
property var quitModel: [ { title: "back"; func: { stackView.pop() } }, { title: "quit"; func: { Qt.quit() } }, ]
-
How to pass function from model to view?
I wrote like below, but error happened with the message [ ListElement: cannot use script for property value].ListModel{ id:quitModel ListElement{ title:qsTr("back") func:stackView.pop();//<---------ERROR } ListElement{ title:qsTr("quit") func:Qt.quit() } } GridView{ model:quitModel anchors.fill: parent delegate:Button{ width:cellWidth text:title onClicked:func } }
-
wrote on 24 Jul 2023, 01:14 last edited by
Sorry everyone, I gave up.
-
-
wrote on 8 Mar 2024, 16:25 last edited by
Take a look at this and see if you can get something out of it.
"Beginning with Qt 5.11 ListElement also allows assigning a function declaration to a role. This allows the definition of ListElements with callable actions."import QtQuick Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Rectangle { anchors.fill: parent ListModel { id: fruitModel property real appleValue: 2.45 property real orangeValue: 3.25 property real bananaValue: 1.95 ListElement { name: "Apple" // cost: 2.45 // cost: fluidModel.appleValue // Error: ListElement: cannot use script for property value cost: function(newCost = 2.45){// value 2.45 could be stored in persistent settings return (fruitModel.appleValue = newCost); } } ListElement { name: "Orange" // cost: 3.25 // cost: fluidModel.orangeValue // Error: ListElement: cannot use script for property value cost: function(newCost = 3.25){ return (fruitModel.orangeValue = newCost); } } ListElement { name: "Banana" // cost: 1.95 // cost: fluidModel.bananaValue // Error: ListElement: cannot use script for property value cost: function(newCost = 1.95){// value could stored in a settings variable return (fruitModel.bananaValue = newCost); } } } Component { id: fruitDelegate Row { spacing: 10 Text { text: name } // Text { text: '$' + cost } // if value Text { text: '$' + cost() } // if function } } ListView { anchors.fill: parent model: fruitModel delegate: fruitDelegate } } }