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. QML ListView in qml is not updated when model changed
Qt 6.11 is out! See what's new in the release blog

QML ListView in qml is not updated when model changed

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.4k 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.
  • L Offline
    L Offline
    lomilomi
    wrote on last edited by
    #1

    I am trying to update a model in a QML ListView from a cpp file..

    this is my model implementation albummodel.h

    
    
    #include <QAbstractListModel>
    #include <QStringList>
    #include <QQmlListProperty>
    
    class Album
    {
    public:
        Album(const QString &type, const QString &size);
    
    
        QString type() const;
        QString size() const;
    
    private:
        QString m_type;
        QString m_size;
    
    };
    
    class AlbumModel : public QAbstractListModel
    {
        Q_OBJECT
        Q_PROPERTY(QQmlListProperty<QObject> myModel NOTIFY listChanged)
    
    public:
    
        enum AlbumRoles {
            TypeRole = Qt::UserRole + 1,
            SizeRole
        };
    
        AlbumModel(QObject *parent = 0);
    
        //QQmlListProperty<QObject> getList();
    
        void addAlbum(const Album &album);
    
        int rowCount(const QModelIndex & parent = QModelIndex()) const;
    
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    
    protected:
        QHash<int, QByteArray> roleNames() const;
    private:
        QList<Album> m_albums;
    signals:
        void listChanged();
    #include <QAbstractListModel>
    #include <QStringList>
    #include <QQmlListProperty>
    
    class Album
    {
    public:
        Album(const QString &type, const QString &size);
    
    
        QString type() const;
        QString size() const;
    
    private:
        QString m_type;
        QString m_size;
    
    };
    
    class AlbumModel : public QAbstractListModel
    {
        Q_OBJECT
        Q_PROPERTY(QQmlListProperty<QObject> myModel NOTIFY listChanged)
    
    public:
    
        enum AlbumRoles {
            TypeRole = Qt::UserRole + 1,
            SizeRole
        };
    
        AlbumModel(QObject *parent = 0);
    
        //QQmlListProperty<QObject> getList();
    
        void addAlbum(const Album &album);
    
        int rowCount(const QModelIndex & parent = QModelIndex()) const;
    
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    
    protected:
        QHash<int, QByteArray> roleNames() const;
    private:
        QList<Album> m_albums;
    signals:
        void listChanged();
    };
    

    This is my albummodel.cpp

    #include "albummodel_mediaplayer.h"
    
    Album::Album(const QString &type, const QString &size)
        : m_type(type), m_size(size)
    {
    }
    
    QString Album::type() const
    {
        return m_type;
    }
    
    QString Album::size() const
    {
        return m_size;
    }
    
    AlbumModel::AlbumModel(QObject *parent)
        : QAbstractListModel(parent)
    {
    }
    
    void AlbumModel::addAlbum(const Album &album)
    {
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_albums << album;
        endInsertRows();
    
        emit listChanged();
    }
    
    int AlbumModel::rowCount(const QModelIndex & parent) const {
        Q_UNUSED(parent);
        return m_albums.count();
    }
    
    QVariant AlbumModel::data(const QModelIndex & index, int role) const {
        if (index.row() < 0 || index.row() >= m_albums.count())
            return QVariant();
    
        const Album &Album = m_albums[index.row()];
        if (role == TypeRole)
            return Album.type();
        else if (role == SizeRole)
            return Album.size();
        return QVariant();
    }
    
    
    QHash<int, QByteArray> AlbumModel::roleNames() const {
        QHash<int, QByteArray> roles;
        roles[TypeRole] = "type";
        roles[SizeRole] = "size";
        return roles;
    }
    
    
    /*QQmlListProperty<QObject> AlbumModel::getList(){
        return QQmlListProperty<QObject>(this, *list);
    }*/
    

    and my qml file

    ListView {
    model: myModel
    
    ....
    }
    

    But the model isn't updated.....Can someone help me to get the connection right?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @lomilomi said in QML ListView in qml is not updated when model changed:

      Q_PROPERTY(QQmlListProperty<QObject> myModel NOTIFY listChanged)

      This is wrong, you don't need any Q_PROPERTY when using a QAbstract*Model.

      In QML, you should simply point model to an instance of AlbumModel. I don't know how you create/ pass it from C++ to QML so I can't exactly tell you how to do it in your case.

      In general, though, setting it through root context property of the QQmlEngine is the easiest route and works well.

      (Z(:^

      L 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @lomilomi said in QML ListView in qml is not updated when model changed:

        Q_PROPERTY(QQmlListProperty<QObject> myModel NOTIFY listChanged)

        This is wrong, you don't need any Q_PROPERTY when using a QAbstract*Model.

        In QML, you should simply point model to an instance of AlbumModel. I don't know how you create/ pass it from C++ to QML so I can't exactly tell you how to do it in your case.

        In general, though, setting it through root context property of the QQmlEngine is the easiest route and works well.

        L Offline
        L Offline
        lomilomi
        wrote on last edited by lomilomi
        #3

        @sierdzio

        This is my main program

        void main() {
              qmlRegisterType<AlbumModel>("AlbumModel",1,0,"AlbumModel");
              Album g("test", "test");
               AlbumModel h();
               h.addAlbum(g);
          }
        

        Qml file

        import AlbumModel 1.0 ListView { model: AlbumModel.myModel delegate: playListThumbnail Component { id: playListThumbnail
        
                    Item {
         Text {
                            id: albumTitleText
                            text:  type//item_title 
        }}
        ....
        }
        
        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You create 2 different instances of AlbumModel: one in C++ and one in QML. Then you add an album in C++ - but obviously the other object in QML won't know about it.

          Solution: do not create 2 objects. For example, in C++:

          void main() {
                Album g("test", "test");
                 AlbumModel h();
                 h.addAlbum(g);
                 engine.rootContext()->setContextProperty("AlbumModel", &h);
            }
          

          With this it should work fine.

          (Z(:^

          1 Reply Last reply
          1
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            This is almost verbatim the same as this example on the wiki

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

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