how to access data from a delegate's component.
-
I have a TabeView object underwhich i have implemented various tableViewcolumns
This is the code
TableView { anchors.fill: parent clip: true model: peopleModel TableViewColumn { id: firstColumn title: "First Name" role: "one"; width: 70; delegate: firstColumnComponent } Component{ id:firstColumnComponent Item { id: toplevelItem property bool textpresent: styleData.value? true : false TextEdit{ text: styleData.value } } } TableViewColumn {title: "Last Name"; role: "two"; width: 70; delegate: secondColoumnComponent} Component{ id:secondColoumnComponent Item { id: secondColoumnparent TextEdit{ text: firstColumn.delegate.textpresent ? styleData.value : "Null" } } }
here in the second column i am trying to access the property 'textpresent; which is in the firstColumn's Delegate.
I tried various combinations in the second columns delegate i am not able to acess the property present inside the componenet.
Is there anyway i can access the property present in the first column's delegate?
-
The model should provide this information for you. Create a role in the model which will return the correct value for any given cell. On QML side, just access that role and display it directly. No need for any
get()
method (which, by the way, is calleddata()
https://doc.qt.io/qt-5/qabstractitemmodel.html#data, notget()
). -
The model should hold the data you need, and you should only be getting it through the model.
With some hacking you may be able to get this info from a delegate, but I strongly advise against doing that. Reason: delegates are controlled by the view. They are created an destroyed on the fly, without your control or knowledge. You may successfully get a value from a delegate now, only for the app to crash at another time. In all of Qt's item views, you should always take the data from the model. If this data is missing from the model - add it there.
-
@sierdzio In order for me to get the Data from the model, i will have to have the row Information right? and when i try to do something like
peopleModel.get(Row) i get an error like this
TypeError: Property 'get' of object QAbstractTableModel(0x77fd10) is not a function
Does this mean i have to implement my own 'get' function in the model?
-
The model should provide this information for you. Create a role in the model which will return the correct value for any given cell. On QML side, just access that role and display it directly. No need for any
get()
method (which, by the way, is calleddata()
https://doc.qt.io/qt-5/qabstractitemmodel.html#data, notget()
).