How to access in QML some dynamic context properties define by setcontextproperty
-
Hello, I have defined at runtime in C++ a number of objects from the same class and I have made each of them available to QML by
calling setContextproperty:@
.
.
const QString NAME_FORMAT = "myClass%1";
QDeclarativeContext * context = view.rootContext();
.
.QVector<MyModel*> list;
for(int i=0; i<var; i++)
{
MyModel* myModel = new MyModel();
list.append(myModel);
context ->setContextProperty(NAME_FORMAT.arg(QString().setNum(i)) , myModel);
}
@var is knew only at runtime.
How can access in QML myModelX base on a index?
Thank you
MaurizioEdit: Please use @ tags around code sections; Andre
-
In QML you access to your context objects with the id you setted in setContextProperty(id, object).
But I think you should write a subclass of QObject that will be your wrapper to C++ data. And in QML you will do something like : myWrapper.getModel(index).
You can also use QAbstractListModel in order to have a custom C++ listmodel available in QML.Here some docs about this :
- "http://doc.qt.nokia.com/main-snapshot/qtbinding.html#calling-functions":http://doc.qt.nokia.com/main-snapshot/qtbinding.html#calling-functions
- "http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#qabstractitemmodel":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#qabstractitemmodel
-
Thank you for your suggestion. I have already tried what you suggested but my problem is that I can't generate the id dynamically in QML. For example if I have a ListView that use a custom delegate that it uses a Pathview , I would like to assign one of the model I have created above to the PathView's model but based on the delegate's index. Do you think this is possible?
tks
-
Try with the javascript function eval():
@
id: eval("myModel" + index)
@ -
Hi,
I am also facing a similar problem in QML. Our page consists of many widgets and each widget's properties are changed dynamically during run time. Hence I created a QObject based controller for each widget and injected it in widget's QML through setContextProperty.
@
MyButton {
Rectangle {
visible:contoller.visible;
Text {
text:contoller.buttonText
}
...
}// controller
class Controller:public QObject {
Q_PROPERTY(bool visible READ visible NOTIFY visibleChanged);
Q_PROPERTY(QString text READ text NOTIFY textChanged);
....
};...
myqmlcontext->setContextProperty("controller",myController);
@This QML widget is of no use without the controller injected into it. So rather than injecting the controller via setContextProperty in C++, I want the QML code to create a component and load the corresponding controller as a context property.
Basic idea is to have this QObject based controllers in a model and expose that model into QML. Since the page will have its contents in vertical/horizontal layout, I can create a ListView and then load the components depending on the model. But when I load components how can I inject the QObject controller as a context property into that particular component, via QML code itself.
Any ideas, suggestions are welcome.
-
I have found the solution to my problem and I hope this could help also the latest problem that has been posted. Basically the main mistake I did was to create a new QDeclarativeView when registering my models. Instead I had to use the QDeclarativeView I created in the main since it was the wrapper of my GUI. After that I wrapped in C++ the creation of my models in a class called ModelManager and I made it available in QML by calling setContextProperty. ModelManager has also a Q_INVOKABLE (or a slot. This is fundamental if you want your QML code to be able to called C++ methods) function "QVariant model(int index)", that gets the name of the model that was registered using setContextProperty and then returns the value of the model by calling QDeclarativeContext::contextProperty. Here is the code
@
QVariant ModelManager::model(int pos)
{
Q_ASSERT(pos < m_models->count());
QDeclarativeContext* ctxt = m_view.rootContext();
QString name = m_models->at(pos)->name().toUtf8();
return ctxt->contextProperty(name);
}
@Tks
MaurizioEdit: Please use @ tags around code sections; Andre