Passing model data from delegate Loader to component in TableView object.
-
Hi,
Firstly, I hope that this post conforms to the rules of this forum as this is my first post. I have a QAbstractTableModel that I am passing into my qml file. To then visualise this object (with a 2d grid), I am using a delegate loader and attempting to pass in the model from the loader to the component although i cant even manage to pass in model.gateType (just a QString) with the following code:
TableView { id: circuitGrid //this is where i pass in the model model: qgui && qgui.gateModel ? qgui.gateModel : null Component { id: gateDelegate GateIcon { gateType : delegateLoader.type implicitWidth: 40 implicitHeight: 40 } delegate: Item { implicitWidth: 40 implicitHeight: 40 Loader { id: delegateLoader anchors.fill: parent property string type : model.gateType sourceComponent: gateDelegate } } }
I am sure there is a much simpler/better way to do this but i have been trying for a while now. The code gives the following error:
ReferenceError: delegateLoader is not defined
-
Hi thanks,
Unfortunately that still gives
ReferenceError: model is not defined -
@jebediahcrunk Is } for GateIcon missing?
Component { id: gateDelegate GateIcon { gateType : delegateLoader.type implicitWidth: 40 implicitHeight: 40 } }
-
Sorry, i took some parts out of my actual code to make it a bit more readable so accidentally deleted that in the process. That is not the issue though.
-
@jebediahcrunk I did similar thing.
model is visible for component and gateType : model.gateType should work. -
I do something like this in my application.
Loader
introduces some not necessarily intuitive behaviour about scoping of properties. I think you have accounted for this by introducing yourtype
property at the loader level as a means of passing the model'sgateType
to your loaded component.However, in my code at least, I do not refer to the loader in my loaded component (I do not even give the loader an ID). Instead I simply access the properties that are defined at the loader level because these now exist in the scope of the loaded component.
In your case, that would mean changing to this:
GateIcon { gateType : type // this is the property defined in `Loader` ... }
-
@jebediahcrunk : is the Loader needed here?
What is discussed here is sharing data via context properties, a system that is not recommended anymore.
-
@GrecKo said in Passing model data from delegate Loader to component in TableView object.:
@jebediahcrunk : is the Loader needed here?
What is discussed here is sharing data via context properties, a system that is not recommended anymore.
That's interesting. What would be recommended now?
I use
Loader
for a case where I have heterogeneous components shown in aListView
. I am aware ofDelegateChooser
as another way of doing this in theory, but I saw some weird behaviour when I tried to use it. I forget the details - I need to have another go sometime - but things like properties such as the model index that should have been accessible were sometimes not available in the delegates. Also, the documentation is unclear as to whether it should even be expected to work with aListView
. (IsListView
"internally based on aDelegateModel
"? How am I supposed to know this?) -
That's interesting. What would be recommended now?
DelegateChooser
Is ListView "internally based on a DelegateModel"? How am I supposed to know this?
Yes. Good question 😬 reading the code can answer that but that's hardly practical.
In practice view taking QAbstractItemModel but also QList, JS arrays are based onDelegateModel
.
This is the case forListView
,TableView
,Repeater
, ...