3

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

2

..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>')
1

Recommended / Built-in Approach: Use the built-in "Field Calculator" tool.

  1. Select the vector layer
  2. Enable edit mode by clicking "Toggle editing"
  3. Click "Open Field Calculator" (only clickable when in edit mode)
  4. Add settings for the new field including fieldname and type: "Text (string)"
  5. Use the concat function. e.g. concat(fieldA,fieldB,fieldC) or to join parts with a space character use concat(fieldA,' ',fieldB,' ',fieldC)

(Using QGIS 2.0.1)

Alternative: Using OpenOffice Calc to edit DBF directly.

My preferred (alternative) solution is to actually edit the DBF file manually using OpenOffice Calc. It avoids the need to write python code.

The first row contains the field heading & type. Rather than creating this manually, I recommend adding the empty column first in QGIS with the correct field type. Then re-open the DBF in OpenOffice and make your updates.

Just don't change any column headers or re-order any rows when editing the DBF.

  • As long as you have a correct FID field (use the field calculator in QGIS before switching to Calc) you can resort your dbf in Calc as often as you like. Just remember to restore the correct order before saving - otherwise your attribute data will be mixed up. – SAnderka Jul 15 '14 at 7:21
  • As @SAnderka pointed out, why go messing with the DBF with another software when you could do the same with the QGIS field calculator? However, for the case where you have to do edits over an over again this is not feasible and an automated approach much more time-saving! – Kay Jul 16 '14 at 18:32
  • @Kay Updated to recommend the built-in method as you suggested. Previously I had problems with the Field Calculator, but it works great now. – python1981 Jul 17 '14 at 1:33
  • Actually, I meant "use the field calculator to update your FID column before switching to Calc". I am a big fan of Calc, to be honest - I did some demographic work a while ago, and changing age groups or counting certain populations is just so much easier in Calc, because there is less of an interface in the way (especially if you're quickly testing various alternatives). This might change with the new field calculator bar in 2.4, tough. – SAnderka Jul 17 '14 at 7:26
  • 1
    I'd like to emphasize that the keyword here is 'Automatically'! The field calculator is not appropiate if you have a dataset which you repeatedly have to edit or add features. Running the field calculator each time I would update this dataset would be annoying! – Kay Jul 17 '14 at 7:56

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.