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.
This commit is contained in:
Felix Geyer 2012-04-25 00:10:06 +02:00
parent 974d4f5807
commit 0d20955920
2 changed files with 14 additions and 0 deletions

View File

@ -264,6 +264,10 @@ void Group::setParent(Group* parent, int index)
if (index == -1) { if (index == -1) {
index = parent->children().size(); index = parent->children().size();
if (parentGroup() == parent) {
index--;
}
} }
if (m_parent == parent && parent->children().indexOf(this) == index) { if (m_parent == parent && parent->children().indexOf(this) == index) {
@ -279,6 +283,7 @@ void Group::setParent(Group* parent, int index)
} }
QObject::setParent(parent); QObject::setParent(parent);
Q_EMIT aboutToAdd(this, index); Q_EMIT aboutToAdd(this, index);
Q_ASSERT(index <= parent->m_children.size());
parent->m_children.insert(index, this); parent->m_children.insert(index, this);
} }
else { else {
@ -286,6 +291,7 @@ void Group::setParent(Group* parent, int index)
m_parent->m_children.removeAll(this); m_parent->m_children.removeAll(this);
m_parent = parent; m_parent = parent;
QObject::setParent(parent); QObject::setParent(parent);
Q_ASSERT(index <= parent->m_children.size());
parent->m_children.insert(index, this); parent->m_children.insert(index, this);
} }

View File

@ -135,6 +135,14 @@ void TestGroup::testSignals()
QCOMPARE(spyAboutToMove.count(), 0); QCOMPARE(spyAboutToMove.count(), 0);
QCOMPARE(spyMoved.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); g2->setParent(root, 0);
QCOMPARE(spyAboutToAdd.count(), 2); QCOMPARE(spyAboutToAdd.count(), 2);
QCOMPARE(spyAdded.count(), 2); QCOMPARE(spyAdded.count(), 2);