QML List View is not updating when the model is updated
-
Hi everyone,
When, when the data is added or removed, qml model doesn't update. But when I change the existing data it updates.
Function that add data:
beginInsertRows(QModelIndex(), rowCount(), rowCount()); ApplicationItem *item = new ApplicationItem; item->id = id; item->iconName = info.value("iconName").toString(); item->visibleName = info.value("visibleName").toString(); item->isActive = info.value("active").toBool(); item->wids.append(wid); if (!desktopPath.isEmpty()) { QMap<QString, QString> desktopInfo = Utils::instance()->readInfoFromDesktop(desktopPath); item->iconName = desktopInfo.value("Icon"); item->visibleName = desktopInfo.value("Name"); item->exec = desktopInfo.value("Exec"); item->desktopPath = desktopPath; } m_Items << item; endInsertRows(); emit itemAdded(); emit countChanged();
and the remove data function:
ApplicationItem *item = findItembyWId(wid); if (!item) return; item->wids.removeOne(wid); if (item->currentActive >= item->wids.size()) item->currentActive = 0; handleDataChangedFromItem(item); if (item->wids.isEmpty()) { if (!item->isPinned) { int index = indexOf(item->id); if (index == -1) return; beginRemoveRows(QModelIndex(), index, index); m_Items.removeAll(item); endRemoveRows(); emit itemRemoved(); emit countChanged(); } }
There isn't special in QML. It's implemented in standard way.
Thank you
-
I found out that row count isn't updating. It's updating in every part of the class except the
rowCount()
function. When I called list size, when a item is appending, it updated, but rowCount() doesn't. Here is the function:int AppModel::rowCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return m_Items.size(); }```
-
I found out that row count isn't updating. It's updating in every part of the class except the
rowCount()
function. When I called list size, when a item is appending, it updated, but rowCount() doesn't. Here is the function:int AppModel::rowCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return m_Items.size(); }```
-
@piervalli It's assumed to clear one. As the reference code I used a tutorial for this app removes one Item. I found that rowCount() always return same value in qml. But it updates in c++. and i used
qDebug() << m_Items.size()
in this function. It also gives me same number. I am confused. what's going on. If you want to see full code it's :header file: https://github.com/yegender124001/avdan-os-dock/blob/master/appmodel.h
definations: https://github.com/yegender124001/avdan-os-dock/blob/master/appmodel.cpp