Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt4.8 assigning a JavaScript function to a property



  • I want to pass a JavaScript function to a component through a property. It looks like this is possible in Qt 5.2 with the var type. Is there any solution for Qt 4.8?



  • I thought this was interesting. While you can't assign a function apparently, you can declare a function when use a component. So below, thing1 will use the somefunc declared in the Thing component. thing2 will use the custom somefunc declared when we declare thing2. Still can't find a better way.

    main.qml:
    @
    import QtQuick 1.0

    Item
    {
    Thing
    {
    id: thing1
    }

    Thing
    {
        id: thing2;
    
        function somefunc()
        {
            console.log("custom func")
        }
    }
    

    }
    @

    Thing.qml:
    @
    import QtQuick 1.0

    Item
    {
    function somefunc()
    {
    console.log("default")
    }

    Component.onCompleted:
    {
        somefunc();
    }
    

    }
    @


Log in to reply