Skip to content

Commit

Permalink
Fixed bug when removing an ancestor of the root index
Browse files Browse the repository at this point in the history
  • Loading branch information
VSRonin committed Mar 19, 2024
1 parent b6c5e93 commit 14d0551
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/rootindexproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ void RootIndexProxyModelPrivate::checkRootRowRemoved(const QModelIndex &parent,
return;
if (!isDescendant(m_rootIndex, parent))
return;
if (m_rootIndex.row() >= first && m_rootIndex.row() <= last) {
if (m_rootIndex.parent()==parent){
if (m_rootIndex.row() >= first && m_rootIndex.row() <= last) {
m_rootRowDeleted = true;
q->beginResetModel();
m_rootIndex=QModelIndex();
}
}
else{
m_rootRowDeleted = true;
q->beginResetModel();
m_rootIndex=QModelIndex();
Expand Down
72 changes: 72 additions & 0 deletions tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,78 @@ void tst_RootIndexProxyModel::bug53MoveCols()
#endif
}

void tst_RootIndexProxyModel::bugRemoveAncestor()
{
#ifdef COMPLEX_MODEL_SUPPORT
auto fillModelForBug = [](QAbstractItemModel *model)
{
model->insertRows(0, 5);
model->insertColumns(0,3);
model->setHeaderData(0, Qt::Horizontal, QStringLiteral("Name"));
model->setHeaderData(1, Qt::Horizontal, QStringLiteral("Quantity"));
model->setHeaderData(2, Qt::Horizontal, QStringLiteral("Weight"));
for (int i = 0; i < model->rowCount(); ++i){
int whQuantity=0;
double whWeight=0.0;
const QModelIndex currParent = model->index(i, 0);
model->setData(currParent, QStringLiteral("Warehouse %1").arg(i+1));
model->insertRows(0, 5, currParent);
model->insertColumns(0,3, currParent);
for (int j = 0; j < model->rowCount(currParent); ++j){
int prQuantity=0;
double prWeight=0.0;
const QModelIndex currChild = model->index(j,0,currParent);
model->setData(currChild, QStringLiteral("Product %1").arg(j+1));
model->insertRows(0, 5, currChild);
model->insertColumns(0,3, currChild);
for (int h=0;h< model->rowCount(currChild); ++h){
const int quantity = ModelTestManager::generateRandomNumber();
const double weight = ModelTestManager::generateRandomNumber();
model->setData(model->index(h,0,currChild), QStringLiteral("Component %1").arg(h+1));
model->setData(model->index(h,1,currChild), quantity);
model->setData(model->index(h,2,currChild), weight);
prQuantity+=quantity;
prWeight+=weight;
}
model->setData(model->index(j,1,currParent), prQuantity);
model->setData(model->index(j,2,currParent), prWeight);
whQuantity+=prQuantity;
whWeight+=prWeight;
}
model->setData(model->index(i,1), whQuantity);
model->setData(model->index(i,2), whWeight);
}
};
ComplexModel baseModel;
fillModelForBug(&baseModel);
RootIndexProxyModel proxyModel1;
new ModelTest(&proxyModel1, &baseModel);
proxyModel1.setSourceModel(&baseModel);
proxyModel1.setRootIndex(baseModel.index(1,0, baseModel.index(1,0))); //Set Root to Warehouse 2 Product 2
QSignalSpy proxyRootChangeSpy(&proxyModel1, SIGNAL(rootIndexChanged()));
QVERIFY(proxyRootChangeSpy.isValid());
QSignalSpy proxyResetSpy(&proxyModel1, SIGNAL(modelReset()));
QVERIFY(proxyResetSpy.isValid());
QSignalSpy proxyAboutToBeResetSpy(&proxyModel1, SIGNAL(modelAboutToBeReset()));
QVERIFY(proxyAboutToBeResetSpy.isValid());

baseModel.removeRow(2,baseModel.index(1,0, baseModel.index(1,0))); //Remove Component 3 Warehouse 2 Product 2
QVERIFY(proxyRootChangeSpy.isEmpty());
QVERIFY(proxyAboutToBeResetSpy.isEmpty());
QVERIFY(proxyResetSpy.isEmpty());
proxyModel1.setRootIndex(baseModel.index(1,0, baseModel.index(2,0))); //Set Root to Warehouse 3 Product 2
QCOMPARE(proxyRootChangeSpy.count(),1);
QCOMPARE(proxyAboutToBeResetSpy.count(),1);
QCOMPARE(proxyResetSpy.count(),1);
baseModel.removeRow(2);//Remove Warehouse 3
QCOMPARE(proxyRootChangeSpy.count(),2);
QCOMPARE(proxyAboutToBeResetSpy.count(),2);
QCOMPARE(proxyResetSpy.count(),2);
#else
QSKIP("This test requires the Qt GUI or GenericModel modules");
#endif
}


void tst_RootIndexProxyModel::bug53MoveRows()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ private Q_SLOTS:
void bug53MoveRows();
void bug53Cols();
void bug53MoveCols();
void bugRemoveAncestor();
void bug60Row();
void bug60Col();

protected:
void compareModels(const QAbstractItemModel *source, const QAbstractItemModel *proxy, const QModelIndex &sourcePar, const QModelIndex &proxyPar);
};
Expand Down

0 comments on commit 14d0551

Please sign in to comment.