Solved QML Repeater doesn't update when its model change in C++?
-
Hello,
QML Repeater doesn't update when its model change in C++ I made two classes with names DataModel and DataProvider, DataProvider is responsible to add one name to DataModel which derived from QAbstractListModel after 2 second after adding new name QML couldn't find any changes so the list never update.
The below link is my simple test code that doing what I explain.
https://ufile.io/4yqbk
This link active for 30 daysI would appreciate any help.
-
The problem is for qml side it needs to use roles that declare in model class otherwise the Qml side doesn't update at all.
-
This is the problem-
void DataModel::insertData(const QString &data) { m_listStr<<data; QModelIndex tl=index(m_listStr.length()-1,0); emit dataChanged(tl,tl); }
dataChanged
is for when existing row changes. Here you are adding a new row so you need to use beginInsertRows and endInsertRows instead. -
Dear @shaan7,
Thanks for you reply but may I know why dataChanged doesn't work? Adding data to a list means change the data am I right? -
From the docs -
This signal is emitted whenever the data in an existing item changes.
In this context, "data" really means an "existing item", not "all data" in the model. So when you add to a list, you're not modifying any existing items, you're adding new ones. -
This post is deleted! -
@shaan7 said in QML Repeater doesn't update when its model change in C++?:
In this context, "data" really means an "existing item", not "all data" in the model
Which signal is for all data changed?
-
@Alien
The docs link @shaan7 gave you shows all the signals. There are separate signals for updating data in existing rows/columns versus ones for inserting/deleting rows/columns. There is not one individual signal which covers any/all of these. Your code should raise the signal(s) correctly corresponding to whatever you are doing. -
Dear @JonB,
Ok but why my setData function doesn't work?
void DataModel::changeData(int row, const QString &str) { if(row<0 || row>=m_listStr.length()) return; QModelIndex modelIndex=index(row,0); setData(modelIndex,QVariant::fromValue(str),Qt::DisplayRole); } /*********************************************************/ bool DataModel::setData(const QModelIndex &index, const QVariant &value, int role) { Q_UNUSED(role) if(index.row()<0 || index.row()>=m_listStr.length()) return false; m_listStr.replace(index.row(),value.toString()); emit dataChanged(index,index); return true; }
-
The problem is for qml side it needs to use roles that declare in model class otherwise the Qml side doesn't update at all.