I used QtDesigner to generate a QDialog
like this:
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")
pulse.valueChanged.connect(calculate_avg_and_quantity)
steps = form.findChild(QSpinBox, "steps")
steps.valueChanged.connect(calculate_avg_and_quantity)
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()
quantity.setText(str(new_quantity))