From 8390f9e80a5d41af0662b325bb1cdb6fb34c40ee Mon Sep 17 00:00:00 2001 From: hjk Date: Wed, 4 May 2016 12:46:05 +0200 Subject: [PATCH] TreeView: Announce sorting tree items This is likely to be overkill but seems to fix a regression introduced by e73a9c1b0df9372: Persistent model indices owned by the selection model of the watch view lose their model pointer and consequently cause error messages to be emitted and potentially cause crashes. Change-Id: I3334ca25bb5e2facf51d03c50647f95f55786498 Reviewed-by: David Schulz --- src/libs/utils/treemodel.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/libs/utils/treemodel.cpp b/src/libs/utils/treemodel.cpp index fbbb31c1c9c..7597ced6f67 100644 --- a/src/libs/utils/treemodel.cpp +++ b/src/libs/utils/treemodel.cpp @@ -713,9 +713,21 @@ void TreeItem::removeChildren() void TreeItem::sortChildren(const std::function &cmp) { if (m_model) { - m_model->layoutAboutToBeChanged(); - std::sort(m_children.begin(), m_children.end(), cmp); - m_model->layoutChanged(); + if (const int n = rowCount()) { + QVector tmp = m_children; + std::sort(tmp.begin(), tmp.end(), cmp); + if (tmp == m_children) { + // Nothing changed. + } else { + QModelIndex idx = index(); + m_model->beginRemoveRows(idx, 0, n - 1); + m_children.clear(); + m_model->endRemoveRows(); + m_model->beginInsertRows(idx, 0, n - 1); + tmp.swap(m_children); + m_model->endInsertRows(); + } + } } else { std::sort(m_children.begin(), m_children.end(), cmp); }