Thursday 31 August 2017

qgis - Auto populate field as concatenation of other fields


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..




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('
Name:
Region:
Höehendifferenz:
Schwierigkeit:
Gefahr:
Erlebnis:
Aufstiegshilfe:
Uphill:
Konflikt:
' +
nameField.text() + '
' + regionField.text() + '
' + altField.text() + '
' +
difficField.text() + '
' + riskField.text() + '
' + uphillField.text() + '
' + valueField.text() + '
' +
shuttleField.text() + '
' + conflField.text() + '
')

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...