@JonB, look at that:
void ObjectsView::makeTree()
{
QStandardItemModel *model;
QMap<QString, QStringList*> expanded;
if(tree->model()){
for (int row = 0; row < tree->model()->rowCount(); row++) {
auto index = tree->model()->index(row, 0);
if(!tree->isExpanded(index)) continue;
auto node = tree->model()->data(index).toString();
auto trimmed = node.left(node.indexOf(' '));
auto list = new QStringList;
expanded.insert(trimmed, list);
for (int child = 0; child < tree->model()->rowCount(index); child++) {
auto childIndex = tree->model()->index(child, 0, index);
if(!tree->isExpanded(childIndex)) continue;
node = tree->model()->data(childIndex).toString();
trimmed = node.left(node.indexOf(' '));
list->append(trimmed);
}
}
tree->model()->removeRows(0, tree->model()->rowCount());
model = qobject_cast<QStandardItemModel*>(tree->model());
}
....
// rest of the code
....
for (int row = 0; row < tree->model()->rowCount(); row++) {
auto index = tree->model()->index(row, 0);
auto node = tree->model()->data(index).toString();
auto trimmed = node.left(node.indexOf(' '));
if(!expanded.keys().contains(trimmed)) continue;
tree->setExpanded(index, true);
if(expanded.value(trimmed)->size()){
auto list = expanded.value(trimmed);
for (int child = 0; child < tree->model()->rowCount(index); child++) {
auto childIndex = tree->model()->index(child, 0, index);
node = tree->model()->data(childIndex).toString();
trimmed = node.left(node.indexOf(' '));
if(list->contains(trimmed)) tree->setExpanded(childIndex, true);
}
}
}
qDeleteAll(expanded.values());
}
and somehow it works:
[image: f615ceb4-cfe8-48d3-ab1f-89056b3d1daf.gif]
Any suggestion for improvement?