How to call dataChanged

The following is the addition of a string class. It is called by code, not a table, and I want it to call dataChanged correctly when adding a new row, although this does not work, the table does nothing. What am I doing wrong?

void MyModel::add(const Person& p) { people.push_back(p); QModelIndex top = createIndex(people.count() - 1, 0, 0); QModelIndex bottom = createIndex(people.count() - 1, 3, 0); emit dataChanged(top, bottom); // emit layoutChanged() if headers changed } 
+6
source share
1 answer

dataChanged only works with existing data, you need to call beginInsertRows() / endInsertRows()

 void MyModel::add(const Person& p) { beginInsertRows(QModelIndex(), people.count(), people.count()); people.push_back(p); endInsertRows(); QModelIndex top = createIndex(people.count() - 1, 0, 0); QModelIndex bottom = createIndex(people.count() - 1, 3, 0); emit dataChanged(top, bottom); // emit layoutChanged() if headers changed } 

That should work. Maybe you don't even need emit dataChanged

+8
source

Source: https://habr.com/ru/post/888099/


All Articles