admin管理员组文章数量:1332345
I have a ListView with a list of JavaScript objects as a model. The delegate needs to respond to click and I want to store click handler attached to model item (action
property):
ListView {
id: idListView
model: [
{
name: "Item 1",
icon: "icon1.svg",
action: function() {
//do stuff
}
},
/* other items */
]
delegate: MyDelegate {
name: modelData.name
icon: modelData.icon
MouseArea {
anchors.fill: parent
onClicked {
modelData.action()
}
}
}
}
But when I click on an item i get
TypeError: Property 'action' of object [object Object] is not a function
What's the proper way to attach function to object and call it?
I have a ListView with a list of JavaScript objects as a model. The delegate needs to respond to click and I want to store click handler attached to model item (action
property):
ListView {
id: idListView
model: [
{
name: "Item 1",
icon: "icon1.svg",
action: function() {
//do stuff
}
},
/* other items */
]
delegate: MyDelegate {
name: modelData.name
icon: modelData.icon
MouseArea {
anchors.fill: parent
onClicked {
modelData.action()
}
}
}
}
But when I click on an item i get
TypeError: Property 'action' of object [object Object] is not a function
What's the proper way to attach function to object and call it?
Share Improve this question asked May 31, 2018 at 11:10 Sergei OusyninSergei Ousynin 1782 silver badges15 bronze badges3 Answers
Reset to default 5You should define function as a QML property. Object
doesn't allow that so you could use ListModel
instead:
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
id: root
visible: true
width:480
height: 640
title: qsTr("Hello World")
ListView {
anchors.fill: parent
spacing: 2
model: ListModel {
ListElement {
name: "Item 1"
property var func: function(){ console.log("Item 1 clicked"); }
}
ListElement {
name: "Item 2"
property var func: function(){ console.log("Item 2 clicked"); }
}
}
delegate: Rectangle {
height: 30
color: "#EFEFEF"
border { width: 1; color: "#CCC" }
width: parent.width
Text {
text: name
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
if(typeof func === "function")
func();
else
console.error("Click handler not defined");
}
}
}
}
}
Another but a bit tricked solution:
import QtQuick 2.11
import QtQuick.Window 2.11
Window {
id: root
visible: true
width:480
height: 640
title: qsTr("Hello World")
ListView {
anchors.fill: parent
spacing: 2
property list<QtObject> arr: [
QtObject {
property string name: "Item 1"
property var func: function(){ console.log("Item 1 clicked"); }
},
QtObject {
property string name: "Item 2"
property var func: function(){ console.log("Item 2 clicked"); }
}
]
model: arr
delegate: Rectangle {
height: 30
color: "#EFEFEF"
border { width: 1; color: "#CCC" }
width: parent.width
Text {
text: modelData.name ? modelData.name : "Undefined item"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
if(typeof modelData.func === "function")
modelData.func();
else
console.error("Click handler not defined");
}
}
}
}
}
Unfortunately it is not possible to store a function inside a ListElement
:
Values must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).
A simple way to call a function from a delegate is to keep the function outside of the model and reference its name in the model:
ListView {
id: idListView
readonly property var actions: {
"action1": function() {
console.log("called action 1!");
},
"action2": function() {
console.log("called action 2!");
}
}
model: [
{
name: "Item 1",
icon: "icon1.svg",
action: "action1"
},
{
name: "Item 2",
icon: "icon2.svg",
action: "action2"
},
/* other items */
]
delegate: MyDelegate {
name: modelData.name
icon: modelData.icon
MouseArea {
anchors.fill: parent
onClicked: {
if (typeof idListView.actions[modelData.action] === "function") {
idListView.actions[modelData.action]()
}
}
}
}
}
I ran into a similar issue. I wanted to have a Drawer
containing a ListView
of Action
s. Some try and error and finally got it running (Qt 5.11). May my code is of use for someone else.
Action {
id: aboutAction
text: "About"
icon.source: "icon_info.png"
}
Drawer {
edge: Qt.LeftEdge
width: Screen.pixelDensity * 100 // mm
height: parent.height
ListView {
id: listView
anchors.fill: parent
model: ListModel {}
delegate: ItemDelegate {
width: parent.width
action: action_
}
}
Component.onCompleted: {
listView.model.append({action_: aboutAction});
}
}
本文标签: qtCalling JavaScript object method from QML ListView delegateStack Overflow
版权声明:本文标题:qt - Calling JavaScript object method from QML ListView delegate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325103a2453562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论