Solved Cannot access model index of ObjectModel inside delegate ?
-
Hi,
I have anObjectModel
which I dynamically populate with model delegates withItem
as root element.The documentation states that
ObjectModel.index : int
This attached property holds the index of this delegate's item within the model.
It is attached to each instance of the delegate.However I cannot seem to get this
index
property. I have triedItem { id: delegateItem ... MouseArea { anchors.fill: parent onClicked: { console.debug("Pressed: " + delegateItem.ObjectModel.index) } } }
But this gives the error
TypeError: Cannot read property 'index' of undefined
Of course, I have also tried to access
index
directly, as I would when using e.g. aListModel
, but that also does not work.Furthermore, the following example always returns -1 when the delegate is pressed:
ObjectModel { id: itemModel Rectangle { height: 30; width: 80; color: "red" MouseArea { anchors.fill: parent onClicked : console.debug(ObjectModel.index) } } Rectangle { height: 30; width: 80; color: "green" MouseArea { anchors.fill: parent onClicked : console.debug(ObjectModel.index) } } Rectangle { height: 30; width: 80; color: "blue" MouseArea { anchors.fill: parent onClicked : console.debug(ObjectModel.index) } } } ListView { anchors.fill: parent model: itemModel }
Any ideas?
-
Thanks to @jonB who got me thinking a bit more, I finally got it to work by adding the following import:
import QtQml.Models 2.10 as QM2
and then using:
console.debug("Pressed: " + delegateItem.QM2.ObjectModel.index)
Although I never declare an
ObjectModel
element inside the current QML file scope, I apparently needed to do the import anyway because I try to access the attached propertyObjectModel.index
. Without the import,ObjectModel
is not defined.Furthermore, I needed to use some import qualifier (here
QM2
) because otherwise, Qt Creator underlines the field names of aListModel
I have in the same QML file as havingInvalid property name
:Note how this qualifier also needs to be used when accessing the attached property
delegateItem.QM2.ObjectModel.index
... -
console.debug("Pressed: " + delegateItem.ObjectModel.index) TypeError: Cannot read property 'index' of undefined
This means that
ObjectModel
is undefined, notindex
. -
@JonB said in Cannot access model index of ObjectModel inside delegate ?:
This means that ObjectModel is undefined, not index
Sure. But how can it be? I invoke the debug statement inside the delegate, upon a click (and therefore the delegate is instantiated). Furthermore, the delegate is appended to the
ObjectModel
, and the delegate is shown correctly inside the view. But when I click it, it gives the above error. How then should I access that attached property within the delegate? -
Thanks to @jonB who got me thinking a bit more, I finally got it to work by adding the following import:
import QtQml.Models 2.10 as QM2
and then using:
console.debug("Pressed: " + delegateItem.QM2.ObjectModel.index)
Although I never declare an
ObjectModel
element inside the current QML file scope, I apparently needed to do the import anyway because I try to access the attached propertyObjectModel.index
. Without the import,ObjectModel
is not defined.Furthermore, I needed to use some import qualifier (here
QM2
) because otherwise, Qt Creator underlines the field names of aListModel
I have in the same QML file as havingInvalid property name
:Note how this qualifier also needs to be used when accessing the attached property
delegateItem.QM2.ObjectModel.index
... -
@Diracsbracket
This is why I find JavaScript allowing you to writesomething.anythingITypeInIncludingMisspellings ...
is scarily dangerous :)