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...
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
If I got you right, you can achieve that by running a Python script. You can follow this workflow:
Open QGIS, load a vector layer and select it (activate it) in the ToC.
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).
Open your QGIS Python console and copy your customized function from 2.
If you want to test the function is working, just call it from the QGIS Python console. This way:
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()) shortcut.setContext(Qt.ApplicationShortcut) shortcut.activated.connect(selectByExpression)
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.