[Qt-qml] QAbstractItemModel and setData / setProperty
Stephen Kelly
steveire at gmail.com
Tue May 24 14:27:24 CEST 2011
vineeth wrote:
> 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.
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.
>
> 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.
>
> 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
More information about the Qt-qml
mailing list