I have implemented a hierarchical qtreeview based on this example: http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
I also use a QSortFilterProxyModel along with the model for filtering
I construct the treeview using an addentry function which is called whenever new data is received by the module. The function is given below:
void cTreeModel::addEntry(QModelIndex& sParentIndex, const tDataID id, cAbstractTreeItem *pParentItem)
{
switch (pParentItem->type()) {
case cAbstractTreeItem::TROOT:
{
cAbstractTreeItem* pAItem = pParentItem->hasEntry(id);
QModelIndex sAItemIndex;
if(nullptr == pAItem)
{
beginInsertRows(QModelIndex(), getRootItem()->childCount(), getRootItem()->childCount());
pAItem = new cATreeItem(id, pParentItem); //essentially m_pParentItem->appendChild(this);
endInsertRows();
}
else
{
pAItem->updateData();
}
sAItemIndex = index(pAItem->row());
addEntry(sAItemIndex, id, pAItem);
break;
}
case cAbstractTreeItem::TA
{
cAbstractTreeItem* pB = pParentItem->hasEntry(id);
if(nullptr == pB)
{
beginInsertRows(sParentIndex, pParentItem->childCount(), pParentItem->childCount());
pB = new cBTreeItem(id, pParentItem); //essentially m_pParentItem->appendChild(this);
endInsertRows();
}
else
{
pB->updateData();
}
QModelIndex sBItemIndex = index(pB->row(), 1, sParentIndex);
addEntry(sBItemIndex, id, pB);
break;
}
case cAbstractTreeItem::TB:
{
cAbstractTreeItem* pTC = pParentItem->hasEntry(id);
if(nullptr == pTC)
{
beginInsertRows(sParentIndex, pParentItem->childCount(), pParentItem->childCount());
pTC = new cCTreeItem(id, pParentItem); //essentially m_pParentItem->appendChild(this);
endInsertRows();
}
else
{
pTC->updateData();
}
QModelIndex sCItemIndex = index(pTC->row(), 2, sParentIndex);
addEntry(sCItemIndex, id, pTC);
break;
}
case cAbstractTreeItem::TC:
{
const tCanGUIData* pData = m_pDataHandler->getEntryByID(id);
if(nullptr == pData)
{
return;
}
break;
}
default:
break;
}
}
This function is called as such:
addEntry(QModelIndex(), DataId, getRootItem());
The structure of data is as follows:
TA
|
| -- TB
| | | |
| | | | -- TC
| | | | -- TC
The addentry functions add entries correctly and send out a datachanged signal but the treeview is not updated.
But if I send out a layoutChanged signal, treeview displays the correct data. Similar case happens if I call beginResetModel and EndResetModel before and after calling addEntry. But in that case another error occurs that when the rate of calling addEntry is too large, if i change selection using arrow keys it crashes in the mapToSource (probably its in the middle of a reset operation and model indices become invalid in the proxy filter)
Any suggestion regarding this?