Use setData() from QML
-
@sierdzio said in Use setData() from QML:
The standard approach in QML is to not use data() and setData(). You should use model.roleName instead - bot for reading and writing.
Ok, got it. But something is not working and I don't understand where and why.
When I read data usingmodel.plotted
or similar, I always getundefined
.
Instead, when I try to replace some data, e.g. withmodel.plotted = true
, the functionsetData()
is never entered (I observed that on debug).If you have to use setData(), this should work:
modelId.setData(modelId.index(row, column), value, role)
You need to create the index from the model. Using just integer won't work.
I've tried also this approach. In my previous response I used
var index = model.index(i, 1)
to iterate through indexes, but without success.May be a problem that my
DataDelegate
is in another QML file? -
DONE!!
I was picking an invalid index using 1 instead of 0 for column. Also the role values was wrong.
Here below the "solution".// Read dmodel.data(dmodel.index(n, 0), DataModel.PlottedRole) // Write: dmodel.setData(dmodel.index(n, 0), checked, DataModel.PlottedRole)
Last question. By using
index(row, column)
is the only way to access to then
item of the list? Or I should implement my "custom getter" like this?DataItem DataModel::get(int row) { return mList.at(row); }
-
@ed_robcont_ said in Use setData() from QML:
DONE!!
I was picking an invalid index using 1 instead of 0 for column. Also the role values was wrong.Thanks for posting.
DataItem DataModel::get(int row) { return mList.at(row); }
Since you're not using the standard approach anyway, I'd say it's up to you. A simple getter like this makes absolute sense.
-
It's working, so it's ok for me. Anyway, I'm still not understanding how the standard approach works.
Task: Modify the temperature field in row 3 when a signal is emitted from another QML component.
How to access this field in the standard way, without usingsetData()
? -
@ed_robcont_ said in Use setData() from QML:
Task: Modify the temperature field in row 3 when a signal is emitted from another QML component.
How to access this field in the standard way, without using setData()?Signal emitted from QML should go to your model. Then model should emit
dataChanged()
and this will update all affected delegates on QML side. -
@sierdzio said in Use setData() from QML:
Signal emitted from QML should go to your model. Then model should emit
dataChanged()
and this will update all affected delegates on QML side.Ok, it works, thank you!
One more thing, it'll be the last I promise.
Actually,DataModel
is the owner of my list. What I want is to add it from another class, i.e.DataServer
. I tried this code but is not working. Any suggestion?@DataServer.cpp
void DataServer::showList() { QVector<DataItem*> list; list.append(new DataItem(...); list.append(new DataItem(...); list.append(new DataItem(...); emit listChanged(QVariant::fromValue(list)); }
@DataModel
void DataModel::setList(QVariant list) { m_list = list; }
@QML
Button { id: btn onPressed: dserver.showList() } DataServer { id: dserver onListChanged: dmodel.setList(list) }
-
void DataServer::setList(QVariant list)
{
m_list = list;
}You don't have
emit dataChanged()
here. Or, in this case, you should probably do:beginResetModel(); m_list = list; endResetModel();
-
@sierdzio said in Use setData() from QML:
You don't have
emit dataChanged()
here. Or, in this case, you should probably do:beginResetModel(); m_list = list; endResetModel();
Yes. No signal
dataChanged()
should be emitted.
I did the same you suggested, but I've getting this error.qrc:/main.qml:21: Error: Unknown method parameter type: QVector<DataItem*>
-
Done, by inserting rows one by one.