I wonder if it is possible using a custom form for field calculation to be carried out by means of a calculate button.

I have 4 input fields, I enter in two fields manualy a number, after pressing the button the rest of the fields are calculated.


Pulse: 100-> By user
average:-> calculation of Pulse/3 
Steps: 234-> By user
quantity:-> calculation of Average * Steps 

Is this possible to make with QT such a form?


I used QtDesigner to generate a QDialog like this:enter image description here

and disabled average and quantity QLineEdits (please note that I did not use any layout - you should use one to make it look nicer!). In QGis the *.ui file is set as form in attributes table and form_open() is used as python init function, see also documentation here: http://docs.qgis.org/2.2/de/docs/training_manual/create_vector_data/forms.html#hard-fa-associating-the-form-with-your-layer

Code to calculate average and quantity when changing pulse or steps values looks like this:

from PyQt4.QtGui import QLineEdit, QSpinBox

form = None
steps = None
pulse = None

def form_open(my_dialog, layer_id, feature_id):
    global form
    global pulse
    global steps

    form = my_dialog
    pulse = form.findChild(QSpinBox, "pulse")
    steps = form.findChild(QSpinBox, "steps")

def calculate_avg_and_quantity():
    average = form.findChild(QLineEdit, "average")
    quantity = form.findChild(QLineEdit, "quantity")

    new_average = pulse.value() / 3.0
    average.setText(str(round(new_average, 2)))

    new_quantity = new_average * steps.value()

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.