Discussion:
QAbstractItemModel and setData / setProperty
vineeth
2011-05-24 11:17:58 UTC
Permalink
Hello,
I am using a C++ QAbstractItemModel as a model for a ListView and have
implemented the setData
<http://doc.trolltech.com/4.7.1/qabstractitemmodel.html#setData>and
items<http://doc.trolltech.com/4.7.1/qabstractitemmodel.html#flags>as
indicated in the documentation to make the items editable.
However, if I call, model.setProperty( ... ) from the delegate I get
TypeError:
Result of expression 'model.setProperty' [undefined] is not a function.
I get the same error, even for model.setData(...) call.
calling model.<rolename> = <new role value> from delegate throws
read-only error.

What is the right procedure to set a role value of a abstract item model
from a QML delegate?

Has anybody experienced this error and pls let me know your inputs in
solving it.
( I have reproduced this error in the AnimalModel
example<http://doc.trolltech.com/4.7.1/declarative-modelviews-abstractitemmodel.html>,
and attached the Qt Pro.)
Kindly share your insights.

Thanks.
--vineeth
Stephen Kelly
2011-05-24 12:27:24 UTC
Permalink
Post by vineeth
Hello,
I am using a C++ QAbstractItemModel as a model for a ListView and have
implemented the setData
<http://doc.trolltech.com/4.7.1/qabstractitemmodel.html#setData>and
items<http://doc.trolltech.com/4.7.1/qabstractitemmodel.html#flags>as
indicated in the documentation to make the items editable.
However, if I call, model.setProperty( ... ) from the delegate I get
Result of expression 'model.setProperty' [undefined] is not a function.
I get the same error, even for model.setData(...) call.
calling model.<rolename> = <new role value> from delegate throws
read-only error.
setProperty comes from QObject. It doesn't have anything to do with the
model API. setData is what you want if using C++.

For QML, QAbstractItemModel::setData is not supported.
Post by vineeth
What is the right procedure to set a role value of a abstract item model
from a QML delegate?
Currently you have to do something yourself. You don't want to couple your
view to the model, so consider creating an additional QObject for QML with
an API like

class ModelDataSetter : public QObject
{
...
Q_INVOKABLE void setModelData(QAbstractItemModel *model,
int row,
int role,
QVariant &data)
{
model->setData(model->index(row, 0), role, data);
}
}

Which you then use in QML:

onClicked : _modelDataSetter.setModelData(ListView.view.model, model.index,
someRole, someElement.text);

or something similar.
Post by vineeth
Has anybody experienced this error and pls let me know your inputs in
solving it.
( I have reproduced this error in the AnimalModel
example<http://doc.trolltech.com/4.7.1/declarative-modelviews-
abstractitemmodel.html>,
Post by vineeth
and attached the Qt Pro.)
Kindly share your insights.
Thanks.
--vineeth
vineeth
2011-05-24 14:15:49 UTC
Permalink
Thanks a lot for pointing this out. I could directly declare a q_invokable
method in the model data and get this working.
--vineeth
Post by Stephen Kelly
Post by vineeth
Hello,
I am using a C++ QAbstractItemModel as a model for a ListView and have
implemented the setData
<http://doc.trolltech.com/4.7.1/qabstractitemmodel.html#setData>and
items<http://doc.trolltech.com/4.7.1/qabstractitemmodel.html#flags>as
indicated in the documentation to make the items editable.
However, if I call, model.setProperty( ... ) from the delegate I get
Result of expression 'model.setProperty' [undefined] is not a function.
I get the same error, even for model.setData(...) call.
calling model.<rolename> = <new role value> from delegate throws
read-only error.
setProperty comes from QObject. It doesn't have anything to do with the
model API. setData is what you want if using C++.
For QML, QAbstractItemModel::setData is not supported.
Post by vineeth
What is the right procedure to set a role value of a abstract item
model
Post by vineeth
from a QML delegate?
Currently you have to do something yourself. You don't want to couple your
view to the model, so consider creating an additional QObject for QML with
an API like
class ModelDataSetter : public QObject
{
...
Q_INVOKABLE void setModelData(QAbstractItemModel *model,
int row,
int role,
QVariant &data)
{
model->setData(model->index(row, 0), role, data);
}
}
onClicked : _modelDataSetter.setModelData(ListView.view.model, model.index,
someRole, someElement.text);
or something similar.
Post by vineeth
Has anybody experienced this error and pls let me know your inputs in
solving it.
( I have reproduced this error in the AnimalModel
example<http://doc.trolltech.com/4.7.1/declarative-modelviews-
abstractitemmodel.html>,
Post by vineeth
and attached the Qt Pro.)
Kindly share your insights.
Thanks.
--vineeth
_______________________________________________
Qt-qml mailing list
http://lists.qt.nokia.com/mailman/listinfo/qt-qml
Stephen Kelly
2011-05-24 14:29:37 UTC
Permalink
Post by vineeth
Thanks a lot for pointing this out. I could directly declare a q_invokable
method in the model data and get this working.
I recommend you don't put it in the model class itself. What happens if you
decide you want sorting and add a proxy model? Add the new API to that too?
That's why you should program to the interface (QAIM) not the
implementation.

Loading...