From 14d0551ace43840ac6311abace8365b758db9120 Mon Sep 17 00:00:00 2001 From: V Ronin Date: Tue, 19 Mar 2024 19:07:42 +0100 Subject: [PATCH] Fixed bug when removing an ancestor of the root index --- src/rootindexproxymodel.cpp | 9 ++- .../tst_rootindexproxymodel.cpp | 72 +++++++++++++++++++ .../tst_rootindexproxymodel.h | 2 +- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/rootindexproxymodel.cpp b/src/rootindexproxymodel.cpp index 3712afd..2b7e8fe 100644 --- a/src/rootindexproxymodel.cpp +++ b/src/rootindexproxymodel.cpp @@ -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(); diff --git a/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.cpp b/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.cpp index 6d5e73f..6820591 100644 --- a/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.cpp +++ b/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.cpp @@ -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() { diff --git a/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.h b/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.h index 4145b6d..4fe5c29 100644 --- a/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.h +++ b/tests/tst_RootIndexProxyModel/tst_rootindexproxymodel.h @@ -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); };