Qt QML ListView not updating when underlying model changes dynamically
I'm stuck trying to I tried several approaches but none seem to work... I'm optimizing some code but This might be a silly question, but I'm having trouble with a QML `ListView` that doesn't seem to update when the underlying model changes dynamically..... I'm using Qt 6.3 and my model is a subclass of `QAbstractListModel`. I have implemented the `beginInsertRows()` and `endInsertRows()` methods correctly, and even tried calling `model->dataChanged()` when the model updates, but the view still doesn't refresh. Here's a simplified version of my model: ```cpp class MyListModel : public QAbstractListModel { Q_OBJECT public: MyListModel(QObject *parent = nullptr) : QAbstractListModel(parent) {} void addItem(const QString &item) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); items.append(item); endInsertRows(); emit dataChanged(index(rowCount()-1), index(rowCount()-1)); // Also tried this } int rowCount(const QModelIndex &parent = QModelIndex()) const override { Q_UNUSED(parent); return items.size(); } QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { if (!index.isValid() || index.row() >= items.size()) return QVariant(); if (role == Qt::DisplayRole) return items.at(index.row()); return QVariant(); } private: QStringList items; }; ``` In my QML, I'm using this model like so: ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 ListView { id: listView width: 200 height: 400 model: myListModel delegate: Text { text: model.data } } ``` I've also ensured that the `ListView` is properly connected to the model in QML, but when I call `addItem()` from C++ after the view has already been displayed, the new items do not appear in the `ListView`. I can see that `addItem()` is being called and the size of the `items` list is increasing, but the `ListView` remains unchanged. Is there something I'm missing in terms of notifying the `ListView` of changes in the model? Any insights or suggestions would be appreciated! Any help would be greatly appreciated! For context: I'm using Cpp on Ubuntu. Any ideas what could be causing this? Could this be a known issue? I'm on macOS using the latest version of Cpp. What am I doing wrong? This is happening in both development and production on Windows 11. Thanks for taking the time to read this! Hoping someone can shed some light on this.