3

Here is part of code:

Repeater{
    model: [["Text A", function(){console.log("hello A")}],
            ["Text B", function(){console.log("hello B")}],
            ["Text C", function(){console.log("hello C")}],
            ["Text D", function(){console.log("hello D")}]]
    delegate: Button{
        text: modelData[0]
        onClicked: modelData[1](); // Type Error
    }
}

I want give different behavior for each button. I think it's supposed to be same as native JavaScript.

var func = function(){
  //...
}
func();

How to do this in QML JavaScript?

BTW, now my solution is:

Repeater{
    model: ["Text A",
            "Text B",
            "Text C",
            "Text D"]
    delegate: Button{
        text: modelData
        onClicked: {
           switch(index)
           {
              cast 0:
                console.log("hello A")
                break;
              cast 1:
                console.log("hello B")
                break;
              cast 2:
                console.log("hello C")
                break;
              cast 3:
                console.log("hello D")
                break;
           }
        }
    }
}
2
  • Could you explain me better? Commented Apr 3, 2017 at 2:20
  • @eyllanesc I want store lambda function's function pointer as variable into an array(model of Repeator) at the moment of component(Button) creation. Then call stored function pointer if Button is clicked. Commented Apr 3, 2017 at 2:26

1 Answer 1

2

This looks like a bug to me, it just won't get to the function through the model interface. Or maybe a design limitation. As far as the function is concerned, it is undefined.

You could work around it like this:

  property var mod :
   [["Text A", function(){console.log("hello A")}],
    ["Text B", function(){console.log("hello B")}],
    ["Text C", function(){console.log("hello C")}],
    ["Text D", function(){console.log("hello D")}]]

  Column {
    Repeater {
      id: rep
      model: mod
      delegate: Button {
        text: modelData[0]
        onClicked: mod[index][1]()
      }
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.