Assignment a property of type function in QML

It is possible to do this in QML:

Item
{
    property var refreshChart: function () {}

    Component.onCompleted:
    {
        refreshChart = function ()
        {
            console.log("Hello!")
        }
    }
}

Also, an event handler can be connected dynamically:

Item
{
    TapHandler {
        id: inputHandler
    }

    Component.onCompleted: {
        inputHandler.tapped.connect(MyScript.jsFunction)
    }
}

or with anonymous function:

function openList()
{
    var list = listSource.getMarkets(sortBy)
    list.filterChanged.connect(function() { console.log("TableModel has been changed.") })
    list.filter = filter
    ml.model = list
}

Anonymous function with two parameters:

var order = market.createLimitOrder(buy, amount, price)
order.failed.connect(function(code, message) { fading.show("The order has filed: %1".arg(message))})

Dynamic binding:

Rectangle {
    id: colorbutton
    width: 200; height: 80;

    color: "red"

    TapHandler {
        id: inputHandler
    }

    Component.onCompleted: {
        color = Qt.binding(function() { return inputHandler.pressed ? "steelblue" : "lightsteelblue" });
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *