Search code examples
c++qtqmlqt-quick

How to iterate QAbstractListModel?


I have a requirement where I have QAbstractListModel which is being updated continuously. The data type of QAbstractListModel is integar type.

I would like to copy the whole data at particular intervals into the vector so that vector is updated continuously and I can use it further.

Any idea how can I iterate QAbstractListModel by its index and copy it into vector.


Solution

  • Quick and dirty way of doing it :

    QAbstractListModel m;
    QVector<int> v;
    
    const int nbRow = m.rowCount();
    v.reserve(nbRow);
    
    for (int i = 0; i < nbRow; ++i)
    {
        int myInt = m.index(i, 0).data().toInt();
        v.append(myInt);
    }