Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListModel in qml, getting objects from model

Tags:

qt

qml

I have a ListModel that I fill in the following way:

property ListModel projects: ListModel {}
property Project currentProject : null

function initialization(){
    var comp = Qt.createComponent("Project.qml");
    var object = comp.createObject(parent,{});
    projects.append(object);
    currentProject = projects.get(0)

}
Component.onCompleted: root.initialization();

And I have a error in currentProject = projects.get(0) line. Error text:

main.qml:14: Error: Cannot assign QObject* to Project_QMLTYPE_0*
like image 486
Dcow Avatar asked Feb 26 '26 20:02

Dcow


1 Answers

When you append your Project object to the ListModel, it is the properties of Project object being added to the ListModel (as ListModel roles), not the Project object itself. So, when you use ListModel.get(), the return object is just a object (QObject* to be exact) with the ListModel roles as properties, but not the Project object.

To be more simple, ListModel is not a container for your Project object. It just store the properties of your Project object.

like image 126
Dickson Avatar answered Mar 01 '26 22:03

Dickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!