PDA

View Full Version : QAbstractTreeModel & dataChanged()



tntcoda
2nd June 2009, 23:02
Hi,

I'm trying to update a QTreeView control when data inside its model is changed. I don't mean edited from the control directly, but instead either changed or added to the model from another point in the program.

So in my QAbstractTreeModel I have



void queue::addJob(...)
{
...
// Update model data

QModelIndex x = this->index(jobs.size(), 0, QModelIndex());
QModelIndex y = this->index(jobs.size(), 5, QModelIndex());

// Update the new row
emit dataChanged(x, y);
}



This however does nothing when data is added to the model. I have implemented: rowCount(), columnCount(), headerData() and data().

Please could someone tell me what im doing it wrong, do I have to implement setData()? Do I have to call insertRows() to add a new row, or does rowCount do this job for me? :confused:

emit reset() works, but thats clearly an inefficient approach with lots of data.

Many thanks,

Jack

shentian
2nd June 2009, 23:35
You have to call QAbstractItemModel::beginInsertRows() before you start adding your data and QAbstractItemModel::endInsertRows() afterwards:


void queue::addJob(...)
{
beginInsertRows(...);
// Update model data
endInsertRows();
}

tntcoda
3rd June 2009, 00:12
Thanks very much, works perfectly now.