<?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 a function as model data?]]></title><description><![CDATA[<p dir="auto">How can we pass a function as data within a model ?<br />
I am trying this way, which doesn't work :(</p>
<p dir="auto">Main.qml:</p>
<pre><code>import QtQuick 2.0
import QtQuick.Window 2.2
Window {
    visible: true
    MyBtnRow {
        btnData:
            [
            { tag:"Jump",   action:function(){console.log (" Jump Called OK!");}  },
            { tag:"Run",     action:(function(){console.log(" Run  Called OK!");} ) },
            { tag:"Hide",    action:function(){console.log( " Hide Called OK!");}  },
        ]
    }
}

</code></pre>
<p dir="auto">MyBtnRow.qml</p>
<pre><code>import QtQuick 2.0

Row {
    property var btnData  // [{ tag:"Name",  action: function() {... } }
    spacing: 3

    Repeater {
        id: rep
        model: btnData
        Btn {
            id: btnId
            width: 60;
            height: 50;
            label.text:  "Do " + modelData.tag
            onActivated: {
                try { modelData.action() } catch (err) {console.log("  catch (err:" +err.message+") in Button "+label.text);};
            }
        }
    }
}


</code></pre>
<p dir="auto">Btn.qml</p>
<pre><code>import QtQuick 2.0
Rectangle {
    id: buttonId
    property alias label: labelTextId
    signal activated;
    border.width: 2

    Text {
        id: labelTextId
        anchors.centerIn: parent
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onPressed: {
            buttonId.activated();
        }
    }
}

</code></pre>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.qt.io/topic/68002/how-to-pass-a-function-as-model-data</link><generator>RSS for Node</generator><lastBuildDate>Tue, 19 May 2026 03:05:44 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/68002.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 07 Jun 2016 13:12:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to pass a function as model data? on Thu, 09 Jun 2016 14:53:19 GMT]]></title><description><![CDATA[<p dir="auto">That's a brilliant and elegant solution!<br />
Thank you bro for spending your precious time with a newbie like me!</p>
]]></description><link>https://forum.qt.io/post/332205</link><guid isPermaLink="true">https://forum.qt.io/post/332205</guid><dc:creator><![CDATA[jdcordoba]]></dc:creator><pubDate>Thu, 09 Jun 2016 14:53:19 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass a function as model data? on Wed, 08 Jun 2016 07:11:15 GMT]]></title><description><![CDATA[<p dir="auto">My dear friend, I'm afraid that QML models do not allow functions as data.</p>
<p dir="auto">Nevertheless, a workaround is not complicated:</p>
<p dir="auto">Main.qml:</p>
<pre><code>
import QtQuick 2.0
import QtQuick.Window 2.2
Window {
    visible: true
    width: mb.width * 1.6
    height: mb.height * 1.6
    MyBtnRow {
        id: mb
        anchors.centerIn: parent
        btnData: [
            {tag:"Jump", action:"elsewhere definded"},
            {tag:"Run", action:"elsewhere definded"},
            {tag:"Hide", action:"elsewhere definded"},
            ]
        callbackList: {
            "Jump": function(){console.log(" Jump Called OK!"); },
            "Hide": function(){console.log(" Hide Called OK!"); },
            "Run": function(){console.log(" Run Called OK!"); },
         }
    }
}

</code></pre>
<p dir="auto">MyBtnRow.qml:</p>
<pre><code>import QtQuick 2.0

Row {
    property var btnData
    property var callbackList  
    spacing: 3
    Repeater {
        id: rep
        model: btnData
        Btn {
            id: btnId
            width: 60;
            height: 50;
            label.text:  "Do " + modelData.tag
            onActivated: {
                  try { callbackList[modelData.tag]() } catch (err) {console.log("  catch (err:" +err.message+") in Button "+label.text);};
            }
        }
    }
}


</code></pre>
<p dir="auto">Best regards</p>
]]></description><link>https://forum.qt.io/post/331920</link><guid isPermaLink="true">https://forum.qt.io/post/331920</guid><dc:creator><![CDATA[jdcordoba]]></dc:creator><pubDate>Wed, 08 Jun 2016 07:11:15 GMT</pubDate></item></channel></rss>