admin管理员组

文章数量:1419235

I'm trying to create a new javascript object in QML.

In javascript I would simply:

var newObject = new Object();

but this is flagged as an error by QtCreator

Can anyone help?

I'm trying to create a new javascript object in QML.

In javascript I would simply:

var newObject = new Object();

but this is flagged as an error by QtCreator

Can anyone help?

Share Improve this question edited Aug 9, 2017 at 21:24 derM 13.7k5 gold badges60 silver badges98 bronze badges asked Aug 9, 2017 at 17:26 readysteadygo2006readysteadygo2006 531 silver badge3 bronze badges 1
  • Could you add some more code? Could you cite the exact error message? I get a feeling that you mixed QML and JS code. Please have in mind that QML contains QML code which is not JS. You can embed JS in QML easily but not just by putting JS anywhere in QML file. Have a look at examples. – Filip Hazubski Commented Aug 10, 2017 at 7:44
Add a ment  | 

1 Answer 1

Reset to default 7

You don't need new. You can just create an empty JS Object like this in a script part (e.g. signal handler, function etc.)

var newObject = {} // In a script.

or as a property:

property var someObject: ({}) // You need to wrap it in paranthesis

If you insist on the new-keyword

var someOtherObject = new Object // In a script

seems to be working, too.

本文标签: Creating a new javascript object in QMLStack Overflow