[solved] How to change values of a ListModel from States when it's in another qml file?
-
wrote on 8 Aug 2011, 06:02 last edited by
Hi, I'm trying to change values in a ListModel from States. The starting point is http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html#set-method
@fruitModel.set(3, {"cost": 5.95, "name":"Pizza"})@
However, what would be the syntax in the example above if the ListModel is in another qml file e.g. TheFruitModel.qml ?
And how can you set these changes from a State?
I'm attempting to do it following this syntax, but somehow it doesn't work:
@StateChangeScript { script: TheFruitModel.set(3, {"cost": 5.95, "name":"Pizza"}) }@
-
wrote on 8 Aug 2011, 06:09 last edited by
fruitModel is an id, not a type. TheFruitModel is a type. When you create an instance of the type, e.g. TheFruitModel{ id: fruitModel }, you can use that id to reference it like in the example.
-
wrote on 8 Aug 2011, 06:13 last edited by
Thanks aalpert, but at least in the example above running in a StateChangeScript I get this error:
@StateChangeScript { script: fruitModel.set(3, {"cost": 5.95, "name":"Pizza"}) }
@ReferenceError: Can't find variable: ...@
-
wrote on 8 Aug 2011, 08:45 last edited by
Where is your fruitModel declared? You can't access ids that are declared in another qml file (with exception of accessing ids that are declared in qml file which agregates current qml file).
-
wrote on 8 Aug 2011, 14:45 last edited by
Do you mean that I would need to 'import The FruitModel.qml'?
The "GridView documentation":http://doc.qt.nokia.com/4.7-snapshot/qml-gridview.html says:
bq. This model can be referenced as ContactModel in other QML files. See "QML Modules":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodules.html#qml-modules for more information about creating reusable components like this.
But a simple example for newbies would be nice since the QML Modules page touches many things and at least for me it's not evident to see what I'm supposed to do.
In the example of this thread fruitModel would be declared in TheFruitModel.qml, while
@fruitModel.set(3, {"cost": 5.95, "name":"Pizza"})@
would be in a different document, say MainPage.qml
fwiw the real example I'm facing is that I want to place the set() method at "OnlineBoard.qml":https://gitorious.org/testdef/testdef/blobs/master/testdef/qml/OnlineBoard.qml , where GridView and delegate are defined, while the ListModel itself is placed in "BoardModel.qml":https://gitorious.org/testdef/testdef/blobs/master/testdef/qml/BoardModel.qml
Thank you for your help!
-
wrote on 8 Aug 2011, 16:21 last edited by
So in your chessboardGrid you have model BoardModel {}.
You can use
@
chessboardGrid.model.set(/.../);
@ -
wrote on 8 Aug 2011, 17:09 last edited by
Yes! This works. Thanks a lot Denis (and aalpert).
-
wrote on 29 Feb 2012, 16:04 last edited by
would be nice to look at that example again but files have been removed