Skip to content

Commit

Permalink
Fix crash when object children iterator is invalidated (#90)
Browse files Browse the repository at this point in the history
Calling QQmlContext::nameForObject can have side effects, creating new child objects via property read functions.
  • Loading branch information
MelodicsPavol authored Dec 14, 2023
1 parent e489235 commit 63b5481
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/src/Scene/Qt/QtItemTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ QString GetObjectName(QObject* object)

QObject* FindChildItem(QObject* object, const QString& name)
{
if (object == nullptr) {
return nullptr;
}

using Index = QObjectList::size_type;
if (auto qquickitem = qobject_cast<const QQuickItem*>(object)) {
for (auto child : qquickitem->childItems()) {
for (Index i = 0; i < qquickitem->childItems().size(); ++i) {
auto child = qquickitem->childItems().at(i);
if (GetObjectName(child) == name) {
return child;
}
Expand All @@ -74,7 +80,8 @@ QObject* FindChildItem(QObject* object, const QString& name)
}
}
} else {
for (auto child : object->children()) {
for (Index i = 0; i < object->children().size(); ++i) {
auto child = object->children().at(i);
if (GetObjectName(child) == name) {
return child;
}
Expand Down

0 comments on commit 63b5481

Please sign in to comment.