Add object to QAbstractListmodel-derived model from QML
-
IMO, then you need the initialise Game too in QML just as you have done for GameModel and then pass it's id to the addGame().
-
Ok, but since I don't know how many times the user is going to click the "New Game" button, I can't initialise it with static code like this:
@Game {
id: something
name: "Of the game"
}@So what I need is the QML equivalent of the following C++ code:
@Game g("Name of the game");
// OR
Game* g = new Game("Name of the game");
@It's completely unclear to me how I do that. Naturally, I could bite the bullet and accept that I need to create the object in C++ and pass the new name, but it seems a bit restricted.
-
Not sure, but i think one way to create the Game object dynamically would be through "Component":http://qt-project.org/doc/qt-5/qml-qtqml-component.html and using "createObject":http://qt-project.org/doc/qt-5/qml-qtqml-component.html#createObject-method and then may be you could pass that object to addGame().
What would be the restrictions you think if you go thru c++ way ?
-
Well, perhaps mainly that the interface to create a new Game is restricted to passing strings to C++.
Also, since I haven't found a way of accessing the elements of the model outside the delegate, I'm unable to manipulate them. I have tried defining an at() method, returning a Game pointer, but this seems unusable in QML.
-
But you can do the business logic in c++ and the QML for just the view.
-
Yep, I guess you're right. I'm still absorbing the philosophy of QML. The way ListView works clearly decouples the business logic.
Nonetheless, I'm struggling to see how I can achieve some things in the presentation layer without accessing the elements in the list somehow. Any tips on how to achieve an at()-like method?
-
Now going through your original way,
- Registering the Game.
- Embed Game in "Component":http://qt-project.org/doc/qt-5/qml-qtqml-component.html.
- Create the Game object dynamically using "createObject":http://qt-project.org/doc/qt-5/qml-qtqml-component.html#createObject-method
- Then store that object in an array . For eg.
@
Component {
id: component
Game {}
}
property var gameArray: []
var obj = component.createObject(myGame)
gameArray.push(obj)
@- Then access it,
@
var obj = gameArray[index]
console.log(obj.color)
@
Edited
-
Oh Sorry for the typo. Thanks for pointing out. I've now edited the original post.