PDA

View Full Version : QTreeView / Model problem - rowCount never called with valid parent



NorthTommy
24th September 2020, 01:55
Hi
I implemented my own item model based on QAbstractTableModel.
The using QTreeView widget ONLY shows all my top level/root items, but no childs or expand indications.
I do NOT get any call on rowCount() with a valid parent. On call with invalid parent I return the number of top level items - works fine first depth.
I though that rowCount is called for each item to check if there are any childs.
DoubleClick... on the row didn't help, but the call should come anyway to show the "+" sign to indicate childs.

Something I missed or oversee... hope for some hints, THX

Here is most important of overritten functions (hope so):



QModelIndex HistoryQueryResultModel::index(int row, int column, const QModelIndex &parent) const {
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}

HistoryQueryResultModelEntry* item;

if (!parent.isValid()) {
// unformtunately index() is const, and createIndex() uses non-const internal pointers to members, which become const because of index() is const
// see https://development.qt-project.narkive.com/r2Uu03QQ/qabstractitemmodel-createindex-void-const
item = const_cast<HistoryQueryResultModelEntry*>(m_entries[row]);
} else {
// unformtunately index() is const, and createIndex() uses non-const internal pointers to members, which become const because of index() is const
item = const_cast<HistoryQueryResultModelEntry*>(static_cast<HistoryQueryResultModelEntry*>(parent.internalPointer())->child(row));
}
if (item) {
return createIndex(row, column, item);
} else {
qt_assert("no item found", __FILE__, __LINE__);
return QModelIndex();
}
}

QModelIndex HistoryQueryResultModel::parent(const QModelIndex &index) const {
if (!index.isValid()) {
qt_assert("invalid index", __FILE__, __LINE__);
return QModelIndex();
}

HistoryQueryResultModelEntry *childItem = static_cast<HistoryQueryResultModelEntry*>(index.internalPointer());
HistoryQueryResultModelEntry *parentItem = childItem->parentItem();

if (!parentItem) {
// we are on root element
return QModelIndex();
}
return createIndex(parentItem->index(), 0, parentItem);
}

int HistoryQueryResultModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return m_entries.size();
} else {
qDebug("call for childs");
HistoryQueryResultModelEntry *childItem = static_cast<HistoryQueryResultModelEntry*>(parent.internalPointer());
if (!childItem) {
qt_assert("invalid internal pointer for child", __FILE__, __LINE__);
}
return childItem->childCount();
}
}

int HistoryQueryResultModel::columnCount(const QModelIndex & parent) const
{
// invalid parent means m_entries[row].columns...
return 4;
}