Fix TreeModel emitting nested QAbstractItemModel signals

This led to an assert in QSFPM because it got confused by the
nested reset signals.

Detected by adding
new QAbstractItemModelTester(this,
QAbstractItemModelTester::FailureReportingMode::Fatal);
to the OutlineModel constructor, and just opening QtCreator on a small
qmake-based project.

Fixes: QTCREATORBUG-30035
Change-Id: I41dbc81b5a2275521ece6b865115e1428e07ecf7
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: hjk <hjk@qt.io>
(cherry picked from commit 9124833a63)
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
David Faure
2023-11-29 10:46:55 +01:00
committed by David Schulz
parent a213ce3875
commit a61f9162f1
3 changed files with 20 additions and 6 deletions

View File

@@ -604,7 +604,7 @@ TreeItem::~TreeItem()
{
QTC_CHECK(m_parent == nullptr);
QTC_CHECK(m_model == nullptr);
removeChildren();
removeChildrenSilently();
}
TreeItem *TreeItem::childAt(int pos) const
@@ -714,6 +714,13 @@ void TreeItem::removeChildren()
}
}
void TreeItem::removeChildrenSilently()
{
if (childCount() == 0)
return;
clear();
}
void TreeItem::sortChildren(const std::function<bool(const TreeItem *, const TreeItem *)> &cmp)
{
if (m_model) {
@@ -1060,6 +1067,13 @@ TreeItem *BaseTreeModel::rootItem() const
}
void BaseTreeModel::setRootItem(TreeItem *item)
{
beginResetModel();
setRootItemInternal(item);
endResetModel();
}
void BaseTreeModel::setRootItemInternal(TreeItem *item)
{
QTC_ASSERT(item, return);
QTC_ASSERT(item->m_model == nullptr, return);
@@ -1067,19 +1081,17 @@ void BaseTreeModel::setRootItem(TreeItem *item)
QTC_ASSERT(item != m_root, return);
QTC_CHECK(m_root);
beginResetModel();
if (m_root) {
QTC_CHECK(m_root->m_parent == nullptr);
QTC_CHECK(m_root->m_model == this);
// needs to be done explicitly before setting the model to 0, otherwise it might lead to a
// crash inside a view or proxy model, especially if there are selected items
m_root->removeChildren();
m_root->removeChildrenSilently();
m_root->m_model = nullptr;
delete m_root;
}
m_root = item;
item->propagateModel(this);
endResetModel();
}
void BaseTreeModel::setHeader(const QStringList &displays)

View File

@@ -39,6 +39,7 @@ public:
void removeChildAt(int pos);
void removeChildren();
void removeChildrenSilently();
void sortChildren(const std::function<bool(const TreeItem *, const TreeItem *)> &cmp);
void update();
void updateAll();
@@ -180,7 +181,8 @@ protected:
void clear();
TreeItem *rootItem() const;
void setRootItem(TreeItem *item);
void setRootItem(TreeItem *item); // resets the model
void setRootItemInternal(TreeItem *item);
TreeItem *itemForIndex(const QModelIndex &) const;
QModelIndex indexForItem(const TreeItem *needle) const;

View File

@@ -219,7 +219,7 @@ void OutlineModel::rebuild()
auto root = new SymbolItem;
if (m_cppDocument)
buildTree(root, true);
setRootItem(root);
setRootItemInternal(root);
endResetModel();
}