Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQAbstractListModel not populating elements to List View
Forum Updated to NodeBB v4.3 + New Features

QQAbstractListModel not populating elements to List View

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 933 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.
  • D Offline
    D Offline
    DevQt
    wrote on 25 Jun 2016, 21:05 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Jun 2016, 21:42 last edited by
      #2

      Hi and welcome to devnet,

      Because you are setting your model on your QQuickView object but loading your qml file in your QtQuick2Application viewer object.

      The code for the example can be found here. You also have it Qt's folder.

      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
      1
      • D Offline
        D Offline
        DevQt
        wrote on 25 Jun 2016, 21:54 last edited by
        #3

        resolved , loaded the qml file as below :

        QQuickView view;
        

        view.setResizeMode(QQuickView::SizeRootObjectToView);
        view.setSource(QUrl("main.qml"));
        view.show();

        thanks for the correction

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Jun 2016, 22:11 last edited by
          #4

          You're welcome !

          Since you have it working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)

          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

          4/4

          25 Jun 2016, 22:11

          • Login

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