Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractListModel not populating data to Listview
Qt 6.11 is out! See what's new in the release blog

QAbstractListModel not populating data to Listview

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 853 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DevQtD Offline
    DevQtD Offline
    DevQt
    wrote on last edited by
    #1

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

    }

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Duplicates

      Closing this one.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved