Abstract ListModel

  • hello,

    i have the following classes :
    #ifndef LISTMODEL_H
    #define LISTMODEL_H

    #include <QAbstractItemModel>
    #include <QObject>

    class ListModel:public QAbstractItemModel

        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();


    Q_DECLARE_INTERFACE(ListModel, "ListModel.YourInterface/1.0")

    #endif // LISTMODEL_H


    #include <QObject>
    #include <QModelIndex>
    #include <QString>
    #include <QAbstractListModel>
    #include "aClass.h"
    #include "listmodel.h"

    class ListModelConcrete: public ListModel
    Q_INTERFACES(ListModel OtherInterface)

    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();

    QList<aClass> m_items;
    static const int aRole;


    #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
    return app.exec&#40;&#41;;
    delete view;

    delete model;
    delete controller;

    return app.exec&#40;&#41;;


    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&) const

    and 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 :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.