Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to pass JS function to a ListModel

How to pass JS function to a ListModel

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Schneidi
    wrote on last edited by
    #1

    Hi, i want to append objects to a ListModel.
    In the delegate there is a MouseArea.onClicked handle.
    This handle should call a function that I want to pass to the model.

    I tried to pass it to an object like this.
    @
    var obj = {"label":"OK","callback":function() {console.log('foobar')}};
    bottonModel.append();
    @

    I can call callback() from obj. That works fine.
    But in the delegate i can't call callback() because "Property 'callback' of object [object Object] is not a function".
    Qml doesn't pass the function as callable JS function. How can I pass a function to a dynamically created ListElement ?

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      The ListElement documentation says:
      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).

      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().

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JvdGlind
        wrote on last edited by
        #3

        Not the best solution, but it is a workaround:

        @import QtQuick 2.2
        import QtQuick.Window 2.1

        Window {
        visible: true
        width: 360
        height: 360

        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"); });
            }
        }
        

        }
        @

        Of course you need to keep model index and function index in sync, which I left out.

        Jeffrey VAN DE GLIND
        Principle Consultant @ Nalys
        www.nalys-group.com

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved