Hi all,
how I can access from within C++ the elements of a ListModel?
I write a application with a ListView. The content is set by an ListModel which is set dynamically during runtime. This is necessary because the number of elements is controlled by an config file loaded by the application.
Any examples I found (Qt examples, google search, forums) uses always Models with fixed number of elements. That’s bad!
At first I used the VisualItemModel like
…
VisualItemModel {
id: myModel
objectName: “myModel”
Rect1 {
…
}
Rect2 {
…
}
But the model doesn’t have any append() functions.
The I changed to use a ListModel like that:
…
ListModel {
id: myModel
objectName: “myModel”
ListElement {
source: “Rect1.qml”
}
ListElement {
source: “Rect2.qml”
}
…
with a delegate like that:
Component {
id: myDelegate
Loader {
source: model.source
}
}
That works fine.
But the big question is how I can reference/get access to the list elements from C++?
Items are only available trough its “objectName”. How I can set the “objectName” of the elements?
From C++ i tried this:
…
QObject* object = m_view->rootObject();
QObject model = object->findChild<QObject>(“myModel”);
if (model)
… // this is working!!
…
QList<QObject*> list = model->findChildren<QObject*>(“rect1”); // “rect1” is the ‘objectName’ in Rect1.qml
if (list) // fails!!! No items where found with that name!!!
…
Have anybody an idea how I can get access to such dynamic created elements?
Thanks a lot…
jackmack
↧