PDA

View Full Version : QAbstractItemView don't refresh after RootItem change in QAbstractItemModel



Odin07
25th June 2020, 08:11
Hello,
i have a problem updating my view (QAbtractItemView) when i change the root item in the model (QAbtractItemModel).

My data forms a tree with some branches serving as RootItems in the model. If I now change the RootItem, the view is not updated. Only if the value of a parameter changes and it emits a signal, that parameter is updated in the view.
If the RootItem changes, I call dataChanged(), which does nothing. I have also called resetInternalData(), which doesn't help either.

13483

Here is my method where I change the RootItem.



void TileContentBaseModel::setRootItem(BaseItem * rootItem)
{
if (RootItem != rootItem)
{
beginResetModel();
if (RootItem)
{
disconnect(RootItem, SIGNAL(changedValue(BaseItem*)), this, SLOT(updateValue(BaseItem*)));
}

RootItem = rootItem;

if (RootItem)
{
connect(RootItem, SIGNAL(changedValue(BaseItem*)), this, SLOT(updateValue(BaseItem*)));
}
endResetModel();

QModelIndex index1 = index(0,0, QModelIndex());
QModelIndex index2 = index(rowCount()-1,columnCount()-1, QModelIndex());

emit dataChanged(index1,
index2,
QVector<int>()
<< Qt::DisplayRole
<< Qt::TextColorRole);
}
}


I am grateful for any advice.

ChristianEhrlicher
27th June 2020, 10:47
begin/endResetModel() should be enough, no need to call dataChanged() afterwards.
Is the view updated when you click on a cell after you changed the root item?

Odin07
1st July 2020, 14:47
If I do not call dataChanged(...) in my setRootItem(...) method, then the dataChanged(const QModelIndex & topLeft,const QModelIndex & bottomRight,const QVector<int> &roles) is not called in the view. Probably I have not implemented something correctly yet.

But I have solved my problem. I had not updated all QModelIndex() objects in the method dataChanged(const QModelIndex & topLeft,const QModelIndex & bottomRight,const QVector<int> &roles) from the view. Or, if I had specified a range of cells, I had an error inside.

Thanks for your answer