Working with QGIS 3.28, I am trying to find out how could one use Python to access and modify the vertices of a polygon that were selected manually (via mouse) using the Vertex Tool.

Despite all my searches throughout Stackexchange question, the QGIS code base, the PyQGIS basic reference material and the PyQGIS API docs, I could not find any mention to whether and how this is possible. One of the rare mentions to more-or-less interfacing with Vertex Tools pro-grammatically is: Access the Vertex Tool (Current Layer). But that one is simply about finding and activating the Vertex Tools programmatically.

Considering that the Vertex Tool already shows a table with coordinates of vertices (including a highlighting of selected vertices) that allows one to edit them, I would imagine that accessing and modifying them via Python is possible and certainly extremely useful. But I see no mentions to how this can be done anywhere.

Is there a way to do so?


1 Answer 1


Interesting question. It's certainly possible, though to be honest, I'm not sure there's a strong use-case. It's a bit hacky, but you can do it like this. Once you activate the vertex tool, right-click an editable feature to open the Vertex Editor dock widget and select a row in the table to highlight a vertex, you need to programmatically access the QTableView object which displays the table of feature vertices. Once you do that, we can use PyQt code to edit the data in a selected row.

Rough but working code snippet:

v_editor = iface.mainWindow().findChild(QDockWidget, 'VertexEditor')
vert_tbl = v_editor.findChildren(QTableView)[0]
sel_model = vert_tbl.selectionModel()
selected_row = sel_model.selectedRows()[0].row()
x_index = vert_tbl.model().index(selected_row, 0)
y_index = vert_tbl.model().index(selected_row, 1)
vert_x = vert_tbl.model().data(x_index)
vert_y = vert_tbl.model().data(y_index)
new_x = 128.505273
new_y = -12.034135
vert_tbl.model().setData(x_index, new_x)
vert_tbl.model().setData(y_index, new_y)

Short screencast of demo results (apologies for the poor quality):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.