7

Does anyone know if there's a way to configure QGIS so that when you plot a new feature (i.e. a polygon), it stays selected by default?

If I have to select it separately, and it happens to fall on top of other features, it is difficult to select the right one using the standard 'Select Features' button.

4

I believe @artwork21 is correct in that you will have to edit the signal when adding a feature in order to select it. But you don't need to create a plugin, you can copy/paste the following code into the Python Console which, for your selected layer, selects the newly created feature each time:

layer = qgis.utils.iface.activeLayer()
# Use active layer

def select(featureAdded):
    layer.setSelectedFeatures([featureAdded])

layer.featureAdded.connect(select)
# Connect "featureAdded" event to "select" function

Tested using QGIS 2.12.3-Lyon.


Creating polygon:

Creating polygon

Finished creating polygon and selected automatically:

Finished and selected


EDIT:

In response to the comments, you can load the project which runs the code on startup by adding a Project macro which can be accessed from the toolbar:

Project > Project Properties > Macros

Note that the code will only work on a specific layer, therefore you can define the name of the layer you want the code to be applied to:

import qgis
from qgis.core import QgsMapLayerRegistry

def select(featureAdded):
    layer.setSelectedFeatures([featureAdded])

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    if layer.name() == "LAYER_NAME":
        qgis.utils.iface.setActiveLayer(layer)

layer = qgis.utils.iface.activeLayer()
layer.featureAdded.connect(select)

Project macro

Make sure to save the project and enable macros by going to the toolbar:

Settings > General > Enable macros

  • This works brilliantly and is exactly what i'm after! Being a complete novice with Python though, do you know how to make it so this bit of code runs every time i open my project? (without having to copy and paste it each time) – JamesD Mar 24 '16 at 13:16
  • @JamesD - You can add code to a Project macro which runs everytime you load the project. The code above only works for a single layer. I'm guessing you want to apply this to more than one layer? – Joseph Mar 24 '16 at 13:32
  • No, for the purposes of this project I would only need it to apply to one editable layer (although the project also includes a number of read only layers) – JamesD Mar 24 '16 at 14:25
  • @JamesD - Edited post =) – Joseph Mar 24 '16 at 15:13
  • 1
    Perfect! Works exactly how i hoped it would and is going to save an awful lot of time for the particular jobs i have in mind – JamesD Mar 24 '16 at 15:27
3

I'm not sure that property exists within the QGIS UI. You may have to write a pyqgis plugin to listen for an edit signal, get the newly created feature id, and select that feature.

Here is the API link for the pyqgis edit signals. There are many different posts on this site and other websites for creating a plugin, getting vector feature id, and selecting feature based on id using pyqgis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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