diff --git a/src/app/qgsattributetabledialog.cpp b/src/app/qgsattributetabledialog.cpp index c2bca62e6934..c9d8ff3c16f6 100644 --- a/src/app/qgsattributetabledialog.cpp +++ b/src/app/qgsattributetabledialog.cpp @@ -833,7 +833,7 @@ void QgsAttributeTableDialog::mActionRemoveSelection_triggered() void QgsAttributeTableDialog::mActionSelectAll_triggered() { - mLayer->selectAll(); + mLayer->selectByIds( mMainView->filteredFeatures(), Qgis::SelectBehavior::SetSelection ); } void QgsAttributeTableDialog::mActionDeleteSelected_triggered() diff --git a/tests/src/app/testqgsattributetable.cpp b/tests/src/app/testqgsattributetable.cpp index 64d89ec13b62..0a03c4e0705a 100644 --- a/tests/src/app/testqgsattributetable.cpp +++ b/tests/src/app/testqgsattributetable.cpp @@ -74,6 +74,7 @@ class TestQgsAttributeTable : public QObject void testEnsureEditSelection(); private slots: void testFetchAllAttributes(); + void testSelectAll(); private: QgisApp *mQgisApp = nullptr; @@ -918,5 +919,39 @@ void TestQgsAttributeTable::testFetchAllAttributes() } +void TestQgsAttributeTable::testSelectAll() +{ + std::unique_ptr< QgsVectorLayer > layer = std::make_unique< QgsVectorLayer >( QStringLiteral( "Point?field=col0:integer&field=col1:integer" ), QStringLiteral( "test" ), QStringLiteral( "memory" ) ); + QVERIFY( layer->isValid() ); + + QgsFeature ft1( layer->dataProvider()->fields(), 1 ); + ft1.setAttributes( QgsAttributes() << 1 << 2 ); + layer->dataProvider()->addFeature( ft1 ); + QgsFeature ft2( layer->dataProvider()->fields(), 2 ); + ft2.setAttributes( QgsAttributes() << 3 << 4 ); + layer->dataProvider()->addFeature( ft2 ); + + layer->removeSelection(); + std::unique_ptr< QgsAttributeTableDialog > dlg( new QgsAttributeTableDialog( layer.get() ) ); + + // select all + dlg->mActionSelectAll->trigger(); + QCOMPARE( layer->selectedFeatures().size(), 2 ); + + // select all with expression matching single feature + const QString filterExpression = QStringLiteral( "col1 > 3" ); + dlg->setFilterExpression( filterExpression, QgsAttributeForm::FilterType::ReplaceFilter ); + dlg->mActionSelectAll->trigger(); + const QList< QgsFeature > features = layer->selectedFeatures(); + QCOMPARE( features.size(), 1 ); + QCOMPARE( features[0].id(), 2 ); + + // select all with expression without matching features + dlg->setFilterExpression( QStringLiteral( "false" ), QgsAttributeForm::FilterType::ReplaceFilter ); + dlg->mActionSelectAll->trigger(); + QCOMPARE( layer->selectedFeatures().size(), 0 ); + +} + QGSTEST_MAIN( TestQgsAttributeTable ) #include "testqgsattributetable.moc"