diff --git a/python/gui/auto_generated/attributetable/qgsattributetableview.sip.in b/python/gui/auto_generated/attributetable/qgsattributetableview.sip.in index 1c6b5bd4fdd..2834cd6fbbc 100644 --- a/python/gui/auto_generated/attributetable/qgsattributetableview.sip.in +++ b/python/gui/auto_generated/attributetable/qgsattributetableview.sip.in @@ -70,6 +70,16 @@ Returns the selected features in the attribute table in table sorted order. %End + void scrollToFeature( const QgsFeatureId &fid, int column = -1 ); +%Docstring +Scroll to a feature with a given ``fid``. + +Optionally a ``column`` can be specified, which will also bring that column into view. + +.. versionadded:: 3.16 +%End + + protected: virtual void mousePressEvent( QMouseEvent *event ); diff --git a/src/app/qgsattributetabledialog.cpp b/src/app/qgsattributetabledialog.cpp index eeb2302a369..384ef8a0093 100644 --- a/src/app/qgsattributetabledialog.cpp +++ b/src/app/qgsattributetabledialog.cpp @@ -627,6 +627,7 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeTable_triggered() { masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) ); mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() ); + mMainView->tableView()->scrollToFeature( action.feature().id(), 0 ); } } @@ -648,6 +649,7 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeForm_triggered() { masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) ); mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() ); + mMainView->tableView()->scrollToFeature( action.feature().id(), 0 ); } } diff --git a/src/gui/attributetable/qgsattributetableview.cpp b/src/gui/attributetable/qgsattributetableview.cpp index 54288e3701f..cc45d0405a5 100644 --- a/src/gui/attributetable/qgsattributetableview.cpp +++ b/src/gui/attributetable/qgsattributetableview.cpp @@ -497,3 +497,20 @@ void QgsAttributeTableView::recreateActionWidgets() } mActionWidgets.clear(); } + +void QgsAttributeTableView::scrollToFeature( const QgsFeatureId &fid, int col ) +{ + QModelIndex index = mFilterModel->fidToIndex( fid ); + + if ( !index.isValid() ) + return; + + scrollTo( index ); + + QModelIndex selectionIndex = index.sibling( index.row(), col ); + + if ( !selectionIndex.isValid() ) + return; + + selectionModel()->setCurrentIndex( index, QItemSelectionModel::SelectCurrent ); +} diff --git a/src/gui/attributetable/qgsattributetableview.h b/src/gui/attributetable/qgsattributetableview.h index d8844d354e1..deab8d3452c 100644 --- a/src/gui/attributetable/qgsattributetableview.h +++ b/src/gui/attributetable/qgsattributetableview.h @@ -89,6 +89,16 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView QList selectedFeaturesIds() const; + /** + * Scroll to a feature with a given \a fid. + + Optionally a \a column can be specified, which will also bring that column into view. + + * \since QGIS 3.16 + */ + void scrollToFeature( const QgsFeatureId &fid, int column = -1 ); + + protected: /**