QGIS – How to Auto Populate Fields by Concatenating Other Fields in QGIS for Efficient Data Management

fields-attributesqgisqt4

I'm searching for a way to automatically combine several field values entered in the feature edit dialogue, resulting in a new column comprised of the former values.

I have, say fields A, B and C that will be edited manually with the edit form, where i.e. 'A-text', 'B-text' and so on, are inserted. Field D should then automatically be filled with the concatenated string: 'A-text, something, B-text, something, C-text, something'.

Is a custom ui-file necessary for this? I found this promissing post How to automatically populate fields instantly? but was not able to adapt it for my purpose..

Best Answer

..digging the WWW I eventually found a solution, which in fact is quite straight forward.

So, here's my solution for the record: 'Name', 'Region',.. correspond to column A, B,.. from above, and the html-markup that is inserted on the fly corresponds to 'something'

QT Designer & Custom Form in QGIS.. Set path to UI-file (TrailsForm.ui and python-script markupForm.py in Layer Properties..

Python-Script (markupForm.py)

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

def formOpen(dialog,layerid,featureid): 
    global nameField
    nameField = dialog.findChild(QLineEdit,"Name")
    global regionField
    regionField = dialog.findChild(QLineEdit,"Region")
    global altField
    altField = dialog.findChild(QLineEdit,"Altitude")
    global difficField
    difficField = dialog.findChild(QLineEdit,"Difficulty")
    global riskField
    riskField = dialog.findChild(QLineEdit,"Risk")
    global uphillField
    uphillField = dialog.findChild(QLineEdit,"Uphill")
    global valueField
    valueField = dialog.findChild(QLineEdit,"Value")
    global shuttleField
    shuttleField = dialog.findChild(QLineEdit,"Shuttle")
    global conflField
    conflField = dialog.findChild(QLineEdit,"Conflict") 
    global descrField
    descrField = dialog.findChild(QPlainTextEdit,"Description")
    nameField.textChanged.connect( newDescr )
    regionField.textChanged.connect( newDescr )
    altField.textChanged.connect( newDescr )
    difficField.textChanged.connect( newDescr )
    riskField.textChanged.connect( newDescr )
    uphillField.textChanged.connect( newDescr )
    valueField.textChanged.connect( newDescr )
    shuttleField.textChanged.connect( newDescr )
    conflField.textChanged.connect( newDescr )

def newDescr():
    descrField.setPlainText('<div id="topic" style="float:left; font-weight:bold; padding-right:10px;">Name:</br>Region:</br>H&ouml;ehendifferenz:</br>Schwierigkeit:</br>Gefahr:</br>Erlebnis:</br>Aufstiegshilfe:</br>Uphill:</br>Konflikt:</div><div id="topic-text" style="width:330px;">' +    
    nameField.text() + '</br>' + regionField.text() + '</br>' + altField.text() + '</br>' +
    difficField.text() + '</br>' + riskField.text() + '</br>' + uphillField.text() + '</br>' + valueField.text() + '</br>' +
    shuttleField.text() + '</br>' + conflField.text() + '</div>')
Related Question