From 0d20955920dc694e9406cd33ae4cbf0ed7bb3050 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Wed, 25 Apr 2012 00:10:06 +0200 Subject: [PATCH] Fix bug where index is wrong when a group is moved to the end from the same parent. This triggered the bug: group->setParent(group->parentGroup()); Then index was group->parentGroup()->children().size() instead of size()-1. --- src/core/Group.cpp | 6 ++++++ tests/TestGroup.cpp | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 064d73d6c..23b0fcdd3 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -264,6 +264,10 @@ void Group::setParent(Group* parent, int index) if (index == -1) { index = parent->children().size(); + + if (parentGroup() == parent) { + index--; + } } if (m_parent == parent && parent->children().indexOf(this) == index) { @@ -279,6 +283,7 @@ void Group::setParent(Group* parent, int index) } QObject::setParent(parent); Q_EMIT aboutToAdd(this, index); + Q_ASSERT(index <= parent->m_children.size()); parent->m_children.insert(index, this); } else { @@ -286,6 +291,7 @@ void Group::setParent(Group* parent, int index) m_parent->m_children.removeAll(this); m_parent = parent; QObject::setParent(parent); + Q_ASSERT(index <= parent->m_children.size()); parent->m_children.insert(index, this); } diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index c4b2fe751..b8128189c 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -135,6 +135,14 @@ void TestGroup::testSignals() QCOMPARE(spyAboutToMove.count(), 0); QCOMPARE(spyMoved.count(), 0); + g2->setParent(root); + QCOMPARE(spyAboutToAdd.count(), 2); + QCOMPARE(spyAdded.count(), 2); + QCOMPARE(spyAboutToRemove.count(), 0); + QCOMPARE(spyRemoved.count(), 0); + QCOMPARE(spyAboutToMove.count(), 0); + QCOMPARE(spyMoved.count(), 0); + g2->setParent(root, 0); QCOMPARE(spyAboutToAdd.count(), 2); QCOMPARE(spyAdded.count(), 2);