I can't find if it is possible to launch a 'Select by expression' query (not to open the dialog) with a keyboard shortcut. I can't find this option in the Settings > Configure shortcuts list...

If you have an idea on this question...

  • Which QGIS version are you using? It's available in QGIS 2.6.1 – Joseph Jan 26 '15 at 13:45
  • 2.6.0! How is shortcut named in Configure shortcuts list? – wiltomap Jan 26 '15 at 13:53
  • Added post including screenshot. It's also available in QGIS 2.2.0 (I just loaded it) so it should be available to you in 2.6.0. – Joseph Jan 26 '15 at 14:00

If I got you right, you can achieve that by running a Python script. You can follow this workflow:

  1. Open QGIS, load a vector layer and select it (activate it) in the ToC.

  2. Observe this function:

    def selectByExpression():
        cLayer = iface.mapCanvas().currentLayer()
        expr = QgsExpression( "\"COD_DANE\"=13" )
        it = cLayer.getFeatures( QgsFeatureRequest( expr ) )
        ids = [i.id() for i in it]
        cLayer.setSelectedFeatures( ids )

    In line 3 I'm setting my own expression. In my case, in the dialog I would put the expression "COD_DANE" = 13, but as we are using Python code, we need to escape double quotes and enclose the expression in double quotes, so my expression becomes "\"COD_DANE\"=13". You should adjust this expression to match your data. Now replace my expression by yours, inside QgsExpression parenthesis (line 3).

  3. Open your QGIS Python console and copy your customized function from 2.

  4. If you want to test the function is working, just call it from the QGIS Python console. This way:

  5. Now that our function is working, we will associate a shortcut to it. For that, copy the following code snippet to the QGIS Python console (as you notice, I've chosen the shortcut CTRL+SHIFT+X, you can adjust it to fit your needs):

    from PyQt4.QtGui import QShortcut, QKeySequence
    from PyQt4.QtCore import Qt
    shortcut = QShortcut(QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_X), iface.mainWindow())

After that, you should be able to select the features that match the expression via the keyboard shortcut CTRL+SHIFT+X.

If you face any trouble, please tell me.

  • Thanks a lot @gcarrillo! In fact, this way is far more complicated than clicking on the button to launch the query in the dialog... I was just looking for a shortcut to replace the button-click. I got my answer: there isn't any! – wiltomap Jan 27 '15 at 8:01

I've included a screenshot to show where the function is in the Configure shortcuts option:

Configure shortcuts

  • Yes, this option lanches the dialog itself but it does not launch the query once written inside the dialog. What I'm looking for is how to launch the query and avoid clicking manually on the button at the bottom-right of the dialog... – wiltomap Jan 26 '15 at 14:44
  • Ahh, my apologies. I would guess you can do this via the Python console. Hopefully others can advise on this issue. – Joseph Jan 26 '15 at 14:50

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.