QGIS – Automatically Zooming to Edited Feature in Attribute Table

attribute-tableeditingfeaturesqgiszoom

I want to edit many features in the attribute table, for which I have to see imagery in the background of the features. To speed the process up, ideally I do not want to use the mouse to click somewhere.

Right now I can edit a field, tab to jump to next field, edit, …
When finished with the last attribute of a feature using tab will jump to the first attribute of the next feature (which is nice), but then I would like to pan/zoom to that feature (Str + P/ Str + J) which is only possible if the entire line is selected.

So either I would like to know how to select a column in the attribute table with keyboard shortcut?

The question was asked in Keyboard shortcut to select entire column in attributes table QGIS already, but my use case is different and I wonder if there is a way for that.

Or alternatively I would like to zoom to that feature automatically.

Best Answer

You can use the script below to open your layer's attribute table with the behavior you describe temporarily implemented.

To run the script, open the Python console, open a new blank editor, paste in the script below, make sure the layer you are working on is the active layer in the TOC panel, then click the green 'Play' button to execute the script. The attribute table for your layer will be opened. Now, every time you tab from the last column of one row to the first cell of the next row, the canvas will zoom automatically to that feature.

lyr = iface.activeLayer()

att_tbl = iface.showAttributeTable(lyr)

tbl_view = att_tbl.findChildren(QTableView)[0]

sel_mod = tbl_view.selectionModel()
    
def row_changed(current, previous):
    feature_id = current.model().data(current, QgsAttributeTableModel.FeatureIdRole)
    iface.mapCanvas().zoomToFeatureIds(lyr, [feature_id])
    
sel_mod.currentRowChanged.connect(row_changed)

Quick & dirty demo which shows the expected behavior:

enter image description here