TreeView: Announce sorting tree items

This is likely to be overkill but seems to fix a regression introduced
by e73a9c1b0d: 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 <david.schulz@theqtcompany.com>
This commit is contained in:
hjk
2016-05-04 12:46:05 +02:00
parent cbc5f22b3b
commit 8390f9e80a

View File

@@ -713,9 +713,21 @@ void TreeItem::removeChildren()
void TreeItem::sortChildren(const std::function<bool(const TreeItem *, const TreeItem *)> &cmp) void TreeItem::sortChildren(const std::function<bool(const TreeItem *, const TreeItem *)> &cmp)
{ {
if (m_model) { if (m_model) {
m_model->layoutAboutToBeChanged(); if (const int n = rowCount()) {
std::sort(m_children.begin(), m_children.end(), cmp); QVector<TreeItem *> tmp = m_children;
m_model->layoutChanged(); 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 { } else {
std::sort(m_children.begin(), m_children.end(), cmp); std::sort(m_children.begin(), m_children.end(), cmp);
} }