11

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.

9

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.

  • Is it possible to do it in qgis 2.10? Is there many differences of 1.8? – Diogo Caribé Oct 16 '15 at 19:49
  • yes it is still possible – Ian Turton Feb 13 '17 at 17:46

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.