QAbstractListModel not populating data to Listview



  • Hi ,

    I am trying to populate a Listview using QAbstractListModel. But, the Listview is not displaying anything on the screen. Can anyone help me to figure out the issue here .......

       Below are my source files : qtquick2applicationviewer.h
    

    //###################################

    #ifndef QTQUICK2APPLICATIONVIEWER_H
    #define QTQUICK2APPLICATIONVIEWER_H

    #include <QtQuick/QQuickView>
    #include<QString>
    #include <QAbstractListModel>
    #include <QStringList>

    class QtQuick2ApplicationViewer : public QQuickView
    {
    Q_OBJECT

    public:
    explicit QtQuick2ApplicationViewer(QWindow *parent = 0);
    virtual ~QtQuick2ApplicationViewer();

    void setMainQmlFile(const QString &file);
    void addImportPath(const QString &path);
    
    void showExpanded();
    

    private:
    class QtQuick2ApplicationViewerPrivate *d;
    };

    class Animal
    {
    public:
    Animal(const QString &type, const QString &size);

    QString type() const;
    QString size() const;
    

    private:
    QString m_type;
    QString m_size;
    };

    class AnimalModel : public QAbstractListModel
    {
    Q_OBJECT
    //Q_PROPERTY(QList<Animal> animals READ getListData NOTIFY animalModelChanged)

    public:
    enum AnimalRoles {
    TypeRole = Qt::UserRole + 1,
    SizeRole
    };

    AnimalModel(QObject *parent = 0);
    
    void addAnimal(const Animal &animal);
    
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    
    //const QList<Animal> getListData() { return m_animals; }
    

    signals:
    void animalModelChanged();

    protected:
    QHash<int, QByteArray> roleNames() const;
    private:
    QList<Animal> m_animals;
    };
    #endif // QTQUICK2APPLICATIONVIEWER_H

    //###############################

    qtquick2applicationviewer.cpp

    //####################################

    #include "qtquick2applicationviewer.h"

    #include <QtCore/QCoreApplication>
    #include <QtCore/QDir>
    #include <QtQml/QQmlEngine>

    class QtQuick2ApplicationViewerPrivate
    {
    QString mainQmlFile;
    friend class QtQuick2ApplicationViewer;
    static QString adjustPath(const QString &path);
    };

    QString QtQuick2ApplicationViewerPrivate::adjustPath(const QString &path)
    {
    return path;
    }

    QtQuick2ApplicationViewer::QtQuick2ApplicationViewer(QWindow *parent)
    : QQuickView(parent)
    , d(new QtQuick2ApplicationViewerPrivate())
    {
    connect(engine(), SIGNAL(quit()), SLOT(close()));
    setResizeMode(QQuickView::SizeRootObjectToView);
    }

    QtQuick2ApplicationViewer::~QtQuick2ApplicationViewer()
    {
    delete d;
    }

    void QtQuick2ApplicationViewer::setMainQmlFile(const QString &file)
    {
    d->mainQmlFile = QtQuick2ApplicationViewerPrivate::adjustPath(file);
    setSource(QUrl::fromLocalFile(d->mainQmlFile));
    }

    void QtQuick2ApplicationViewer::addImportPath(const QString &path)
    {
    engine()->addImportPath(path);
    }

    void QtQuick2ApplicationViewer::showExpanded()
    {
    #if defined(Q_WS_SIMULATOR)
    showFullScreen();
    #else
    show();
    #endif
    }

    Animal::Animal(const QString &type, const QString &size)
    : m_type(type), m_size(size)
    {
    }

    QString Animal::type() const
    {
    return m_type;
    }

    QString Animal::size() const
    {
    return m_size;
    }

    AnimalModel::AnimalModel(QObject *parent)
    : QAbstractListModel(parent)
    {
    }

    void AnimalModel::addAnimal(const Animal &animal)
    {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_animals << animal;
    endInsertRows();
    }

    int AnimalModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_animals.count();
    }

    QVariant AnimalModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_animals.count())
    return QVariant();

    const Animal &animal = m_animals[index.row()];
    if (role == TypeRole)
    	return animal.type();
    else if (role == SizeRole)
    	return animal.size();
    return QVariant();
    

    }

    QHash<int, QByteArray> AnimalModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[SizeRole] = "size";
    return roles;
    }

    //###################################
    main.qml

    ListView {
    width: 200; height: 250

    model: myModel
    delegate: Text { text: "Animal: " + type + ", " + size }
    

    }

    // main.cpp

    // ##################
    #include "qtquick2applicationviewer.h"
    #include <QtGui/QGuiApplication>
    #include <QQmlContext>

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    AnimalModel model;
    //model = new AnimalModel();
    
    model.addAnimal(Animal("Wolf", "Medium"));
    model.addAnimal(Animal("Polar bear", "Large"));
    model.addAnimal(Animal("Quoll", "Small"));
    
    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", &model);
    
    //qmlRegisterType<model>("com.example", 1, 0, "myModel");
    
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("main.qml"));
    
    
    viewer.showExpanded();
    
    return app.exec();
    

    }

    //####################


  • Lifetime Qt Champion

    Hi,

    Please don't post the same thread in multiple sub-forum, one is enough.

    Duplicates

    Closing this one.


Log in to reply
 

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