Saturday 17 June 2017

qgis - How to automatically populate fields instantly?


I am looking to create an attribute table where the first field is a drop-down menu (I have achieved this by using the 'value map' edit widget), but I'm struggling to find a method for the next part;


After the value has been selected from the drop-down menu, I would like the adjacent cell (in the next field) to be automatically populated according to the value in the drop-down menu. So for example; if drop-down value = A, then adjacent field = 1. Or if drop-down value = B, then adjacent field value = 3, etc.


I realise that by using field calculator, it is possible to auto-populate fields as part of a separate process, but I would like to have the fields populated at the same time as the drop-down value is selected if possible.


I am using windows 7 and QGIS version 1.8.


Many thanks.




Answer



You will need a little python to do this.


You can read this very nice blog post here


Create a script similar to the following called FillForm.py and place it next to your project


from PyQt4.QtCore import *
from PyQt4.QtGui import *

aField = None
bField = None


def formOpen(dialog,layerid,featureid):
global aField
aField = dialog.findChild(QComboBox,"fieldA")
global bField
bField = dialog.findChild(QLineEdit,"fieldB")
aField.currentIndexChanged.connect( aChanged )

def aChanged( value ):
if ( aField.currentText() == 'A' )
bField.setText( '1' )

elif ( aField.currentText() == 'B' )
bField.setText( '2' )

Go to Vector Layer Properties => General and enter FormFill.formOpen in the Init function line edit.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...