Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call dataChanged

The following is my add a row class. It is called by the code, not the table and I want it to properly call dataChanged when a new row is added, although this isn't working, the table doesn't do anything. 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
}
like image 460
Will03uk Avatar asked May 14 '11 11:05

Will03uk


1 Answers

dataChanged works only on 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. May be you don't even need to emit dataChanged

like image 148
pure cuteness Avatar answered Sep 23 '22 02:09

pure cuteness