0

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.

Example:

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?

1

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")
    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))
| improve this answer | |

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.