PDA

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



NorthTommy
24th September 2020, 09:57
Hi
've got created an own item model based on QAbstractTableModel.
Internal data forms a tree structure with multiple top level items.
The connected QTreeView widget shows all my root level items fine, but NO CHILDS (not at least "+" indicator).

I get NO CALL FOR rowCount() with a parent index other than invalid (root).

I expected Qt would call rowCount for all items to check if there are child.
What do I wrong or what is the misunderstanding?

Hope for some hints. Thx

Here is the important part of my model (data() is also implemented but not shown here).


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;
}

NorthTommy
24th September 2020, 14:48
Found the problem!
I inherit from QAbstractTableModel using it with a treeView as well. BUT the QAbstractTableModel overrides hasChildren() and sets NeverHasChild flags for indices for performance.
I need to inherit from QAbstractItemModel.
Small thing - big repercussion.

d_stranz
24th September 2020, 19:30
inherit from QAbstractTableModel using it with a treeView as well.

A table is not a tree. Table model rows have neither parents nor children.