<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to pass JS function to a ListModel]]></title><description><![CDATA[<p dir="auto">Hi, i want to append objects to a ListModel.<br />
In the delegate there is a MouseArea.onClicked handle.<br />
This handle should call a function that I want to pass to the model.</p>
<p dir="auto">I tried to pass it to an object like this.<br />
@<br />
var obj = {"label":"OK","callback":function() {console.log('foobar')}};<br />
bottonModel.append();<br />
@</p>
<p dir="auto">I can call callback() from obj. That works fine.<br />
But in the delegate i can't call callback() because "Property 'callback' of object [object Object] is not a function".<br />
Qml doesn't pass the function as callable JS function. How can I pass a function to a dynamically created ListElement ?</p>
]]></description><link>https://forum.qt.io/topic/42697/how-to-pass-js-function-to-a-listmodel</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 16:01:47 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/42697.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 19 Jun 2014 13:50:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to pass JS function to a ListModel on Thu, 26 Jun 2014 10:15:04 GMT]]></title><description><![CDATA[<p dir="auto">Not the best solution, but it is a workaround:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> QtQuick 2.2<br />
import QtQuick.Window 2.1</p>
<p dir="auto">Window {<br />
visible: true<br />
width: 360<br />
height: 360</p>
<pre><code>ListModel {
    property var functions: [];

    id: bottonModel
}

ListView {
    anchors.fill: parent
    model: bottonModel
    delegate: MouseArea {
        width: 50
        height: 50

        Rectangle {
            anchors.fill: parent
            color: "red"

            radius: 5

            Text {
                anchors.fill: parent
                text: label

                horizontalAlignment: Text.AlignHCenter
            }
        }

        onClicked: {
            bottonModel.functions[index]();
        }
    }

    Component.onCompleted: {
        bottonModel.append({"label":"OK"});
        bottonModel.functions.push(function() {console.log("foobar"); });
    }
}
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">Of course you need to keep model index and function index in sync, which I left out.</p>
]]></description><link>https://forum.qt.io/post/233545</link><guid isPermaLink="true">https://forum.qt.io/post/233545</guid><dc:creator><![CDATA[JvdGlind]]></dc:creator><pubDate>Thu, 26 Jun 2014 10:15:04 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass JS function to a ListModel on Sat, 21 Jun 2014 02:31:14 GMT]]></title><description><![CDATA[<p dir="auto">The ListElement documentation says:<br />
<em>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).</em></p>
<p dir="auto">If you can't find another way around it, the function can be passed as text, and transformed into a function within the delegate via eval().</p>
]]></description><link>https://forum.qt.io/post/232748</link><guid isPermaLink="true">https://forum.qt.io/post/232748</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Sat, 21 Jun 2014 02:31:14 GMT</pubDate></item></channel></rss>