Iam designing a qml page which consists of 3 lists. I want the data to be displayed in these lists as model from cpp. Can i have all these 3 models as properties from a single class.
I have a class derived from qabstractlistmodel to use as model. I want this model as a property from another class which is bind to qml using qqmlcontextproperty.
ie. I could be able to access this model as a property.
class ToDoModel : public QAbstractListModel
{
Q_OBJECT
...
}
class HelperClass : public QObject
{
Q_OBJECT
Q_PROPERTY(ToDoModel todoModel READ todoModel CONSTANT)
public:
explicit HelperClass(QObject *parent = nullptr);
ToDoModel* todoModel() const;
signals:
public slots:
private:
ToDoModel *_todoModel;
};
int main(int argc, char *argv[])
{
HelperClass helperClass;
engine.rootContext()->setContextProperty(QStringLiteral("helperClass"), &helperClass);
...
}
It shows the error :
Unable to handle unregistered datatype 'ToDoModel' for property 'HelperClass::todoModel'
A QObject, like the QAbstractListModel, is not copied, so in that case you must return the pointer. So in general if T is a QObject then if you expose it as a property it must be Q_Property(T* name ...)
.
So in your case it changes to:
class HelperClass : public QObject
{
Q_OBJECT
Q_PROPERTY(ToDoModel* todoModel READ todoModel CONSTANT)
// ...