3

I'm very new to QGIS and Python (although years ago had some programming experience with C++), but have been muddling through with a fairly large project cataloging cemetery plots at the moment.

I currently have a custom form used for taking photos and cross referencing existing data, taking a photo and linking with physically surveyed plots.

What I'm hoping to do is take a few steps out of taking a photo and linking it with an existing record. What currently happens is, the user switches to the camera app, takes the photo, and then comes back into QGIS to search for the file and link to the record in the custom form.

What I would like to do is have a button in my custom form that when selected launces the camera app, and once closed automatically looks for the newest file in the camera directory. I have managed to achieve this to a certain extent in the python console with the following:

import os
import subprocess

subprocess.call(['notepad.exe'])
path="C:\\temp\\"
file_list=os.listdir(path)
dated_files = [(os.path.getctime(path + fn), os.path.basename(path + fn)) for fn in file_list]
dated_files.sort()
dated_files.reverse()
newest = dated_files[0][1]
return_string= path + newest

Obviously the notepad.exe call and path variable will be updated once on the data collection computer, but this seems to work for testing so far...

I am now struggling with how this would be put into the script for "Python Init Function" in QGIS so that it runs when a button box is pressed and then modifies the value within the form.

1

Here is my example code. To use the code I prepared a QDialog in QtDesigner with a simple QLineEdit named "field" and a QPushButton named "push" - In the form_open() function you have to find these components. Afterwards, clicked signal of the button is connected to function. Try to enter your code in function check_and_set_field_value():

from PyQt4.QtGui import QLineEdit, QPushButton

form = None
field = None


def form_open(my_dialog, layer_id, feature_id):
    global form
    global field

    form = my_dialog
    field = form.findChild(QLineEdit, "field")
    push = form.findChild(QPushButton, "push")
    push.clicked.connect(check_and_set_field_value)


def check_and_set_field_value():
    # Your code goes here...

    # Example return value:
    return_value = 'test'

    if return_value is not None:
        field.setText(return_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.