Can I use a pointer in the ListView?
-
As is description in my last post "http://developer.qt.nokia.com/forums/viewthread/8638/":http://developer.qt.nokia.com/forums/viewthread/8638/
A new Page Will be created dynamically, and every Page will have its own C++ model to provide data for it.
But we must use this kind of way to expose my model object to the QML context:@QDeclarativeContext *ctxt = view.rootContext();
ctxt->setContextProperty("myModel", &model);@
Because the C++ model will also created dynamically. And every name string of a model expose to QML, as is string "mymodle"here, must be unique. So how Can create my C++ model dynamically and expose to QML to use? Can I expose the pointer of the model not the string name "mymodel" to QML?
For example, I have a button. If I push the button 10 times, it will create 10 model for me and expose to the QML to use. Certainly, we cannot use "mymodel" in all the model created dynamically. If I transfer the pointer of the C++ model to the ListView, not by the string name "mymodel", it will be a very good thing.
Thanks.
-
Hi,
Would something like the following work in your case?
@
Page {
//assign the model to this when it is created, rather than creating a property on the root context.
property variant myModelListView { model: myModel }
}
@Regards,
Michael -
[quote author="mbrasser" date="1313020617"]Hi,
Would something like the following work in your case?
@
Page {
//assign the model to this when it is created, rather than creating a property on the root context.
property variant myModelListView { model: myModel }
}
@Regards,
Michael[/quote]you mean I can assign the pointer of my C++ model to the property variant myModel?
-
Yes, for example something like:
@
Page {
property variant myModelListView { model: myModel } Button { onClicked: myModel = myApp.createModel() }
}
@where createModel returns a pointer to your model object.
Regards,
Michael -
[quote author="mbrasser" date="1313030598"]Yes, for example something like:
@
Page {
property variant myModelListView { model: myModel } Button { onClicked: myModel = myApp.createModel() }
}
@where createModel returns a pointer to your model object.
Regards,
Michael[/quote]I use it as below:
@
Rectangle{
property variant mymodel
ListView {
//...other codesdelegate: mydelegate model: mymodel cacheBuffer: 40 } Component.onCompleted: { mymodel = myApp.createModel() console.log("load model complete") }
}
@But there is no data on the UI. I don't know why.
-
Have you verified that mymodel is being set correctly (by e.g. console.logging it)? You might need to register your model type (or have the signature of createModel be QObject *createModel() ) to get things working correctly.
Note that you could also assign directly to the ListView's model (mymodel isn't strictly necessary), e.g myListView.model = myApp.createModel().
Regards,
Michael -
[quote author="mbrasser" date="1313041822"]Have you verified that mymodel is being set correctly (by e.g. console.logging it)? You might need to register your model type (or have the signature of createModel be QObject *createModel() ) to get things working correctly. Note that you could also assign directly to the ListView's model (mymodel isn't strictly necessary), e.g myListView.model = myApp.createModel(). Regards, Michael[/quote]
The model that created is c++ model.
It works well as below:
@
DataModel *p = new DataModel;
rootContext->setContextProerty("myModel", p);
//Then, reference myModel in the ListView, it works well
@So. in some degree, I let myApp to create the model:
@
//DataMode is a subclass of the QAbstractListModel, it works well.
DataModel * myApp::createModel()
{
return new DataModel;
}
@
So, should I regist my model type? -
Yes, I think both of those things are worth trying (registering the type or changing the signature as mentioned above) to see if that gets things working for you (though I can't say for certain that that is the problem).
Regards,
Michael -
Hi Michael,
Thanks for your reply. The thought you provide to me is very important for me.
And I get over it at last.
I just regist my Model Type, and all is well now.Thanks. :)