Abstract ListModel
-
hello,
i have the following classes :
listmodel.h
@
#ifndef LISTMODEL_H
#define LISTMODEL_H#include <QAbstractItemModel>
#include <QObject>class ListModel:public QAbstractItemModel
{
Q_OBJECT
public:virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const=0 ; virtual int rowCount(const QModelIndex &parent = QModelIndex()) const=0; virtual QHash<int, QByteArray> roleNames() const=0; virtual ~ListModel(); private: Q_DISABLE_COPY(ListModel);
};
Q_DECLARE_INTERFACE(ListModel, "ListModel.YourInterface/1.0")
#endif // LISTMODEL_H
@listmodelconcrete.h
@
#ifndef LISTMODELCONCRETE_H
#define LISTMODELCONCRETE_H#include <QObject>
#include <QModelIndex>
#include <QString>
#include <QAbstractListModel>
#include "aClass.h"
#include "listmodel.h"class ListModelConcrete: public ListModel
{
Q_INTERFACES(ListModel OtherInterface)public:
ListModelConcrete(QObject *parent=0);QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const ; int rowCount(const QModelIndex &parent = QModelIndex()) const; QHash<int, QByteArray> roleNames() const; void populate();
private:
Q_DISABLE_COPY(ListModelConcrete);
QList<aClass> m_items;
static const int aRole;
};#endif // LISTMODELCONCRETE_H
@main.cpp
@
#include <QtGui/QGuiApplication>
#include <QtQml>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQuickView>#include "listmodelconcrete.h"
#include "controllerconcrete.h"int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView *view=new QQuickView();qDebug()<<"view created "; ListModelConcrete *model=new ListModelConcrete(); qDebug()<<"model created "; ControllerConcrete *controller=new ControllerConcrete(); qDebug()<<"ControllerConcrete created "; view->engine()->rootContext()->setContextProperty("myModel",model); // lm is a ListModel declared in controller.h //view->engine()->rootContext()->setContextProperty("myController",controller); view->setSource(QUrl::fromLocalFile("qml/MVC/main.qml")); view->show(); return app.exec(); delete view;
delete model;
delete controller;return app.exec();
}
@with this error as return:
error: cannot allocate an object of abstract type 'ListModelConcrete'
note: because the following virtual functions are pure within 'ListModelConcrete':
note: virtual QModelIndex QAbstractItemModel::index(int, int, const QModelIndex&) constand the same for the other 2 functions.
although i did implement them in ListModel...it doesnt work, either, if the @QModelIndex QAbstractItemModel::index(int, int, const QModelIndex&) const@ is declared like this.
error on line 20 in main.cpp
please help.
-
From the documentation for QAbstractItemModel:
"When subclassing QAbstractItemModel, at the very least you must implement index(), parent(), rowCount(), columnCount(), and data(). These functions are used in all read-only models, and form the basis of editable models".
It does not seem to me that you are doing this in your files. Specifically, it doesn't seem that you are implementing index(...) or parent(...).
As a hint, Qt provides an abstract list model if that's what you want, "QAbstractListModel":http://qt-project.org/doc/qt-5/qabstractlistmodel.html, that could perhaps ease some of this work for you.
-
i have to create:
-an abstract model which inherits QAbstractItemModel
-a concrete listmodel which inherits the model above
-an abstract controller (i have no idea if this one must inherit something)
-and a concrete controller which inherits the controller above.thing is...I simply can not implement the pure virtual functions of QAbstractItemModel, so i could create the model in main.cpp and work with it in QML.
point me some directions, please.
thank you :)