From 502dfcf352b0094e2b1e8b495af1cdeb3e7874ed Mon Sep 17 00:00:00 2001 From: Cristian Maureira-Fredes Date: Wed, 25 Mar 2020 11:45:21 +0100 Subject: Invert QTreeWidgetItem clear function loop When using clear on an item with children, we rely on the childCount() function so when children with only a parent and no local reference were inside, they ended up being removed, altering the behavior of the loop. Change-Id: I111a600cd2e805eeb7110082437e666f88ff65a5 Fixes: PYSIDE-1251 Reviewed-by: Friedemann Kleint --- sources/pyside2/PySide2/glue/qtwidgets.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sources/pyside2/PySide2/glue/qtwidgets.cpp b/sources/pyside2/PySide2/glue/qtwidgets.cpp index 1c663364c..4f9baadf1 100644 --- a/sources/pyside2/PySide2/glue/qtwidgets.cpp +++ b/sources/pyside2/PySide2/glue/qtwidgets.cpp @@ -413,7 +413,15 @@ for (auto *item : items) { // @snippet qtreewidget-clear QTreeWidgetItem *rootItem = %CPPSELF.invisibleRootItem(); Shiboken::BindingManager &bm = Shiboken::BindingManager::instance(); -for (int i = 0, i_count = rootItem->childCount(); i < i_count; ++i) { + +// PYSIDE-1251: +// Since some objects can be created with a parent and without +// being saved on a local variable (refcount = 1), they will be +// deleted when setting the parent to nullptr, so we change the loop +// to do this from the last child to the first, to avoid the case +// when the child(1) points to the original child(2) in case the +// first one was removed. +for (int i = rootItem->childCount() - 1; i >= 0; --i) { QTreeWidgetItem *item = rootItem->child(i); if (SbkObject *wrapper = bm.retrieveWrapper(item)) Shiboken::Object::setParent(nullptr, reinterpret_cast(wrapper)); -- cgit v1.2.3