1

I'm working on this answer from @Nathan W to find how to deal with missing files, as they return a error message if QGIS doesn't find any.

The initial workflow is to be able to click on an object (here a point which is a borehole) and open the related crosscut, that I have renamed from the borehole name. If the crosscut doesn't exist, I'd like to display a message saying this crosscut is not referenced. All the images are in jpg format and are in a folder named "Fichiers_Coupes_SCAN" ad my image name is in the attribute table in a column named "ID_SONDAGE".

My code is the following :

from os import startfile
from os.path import abspath, dirname, join, isfile
from PyQt4.QtGui import QMessageBox
proj = QgsProject.instance()
urfile = str(proj.fileName())
path = join(abspath(dirname(urfile)),"Fichiers_Coupes_SCAN", "[% "ID_SONDAGE" %]"".jpg")
import os.path
if os.path.isfile(path) :
    startfile(path)
else:
    QMessageBox.information(None, "Error : ","Can't find the crosscut")

I'd like the error message to display the name of the borehole, but I must do something wrong and I get error messages when inserting the variables.

closed as off-topic by underdark Apr 20 '17 at 17:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – underdark
If this question can be reworded to fit the rules in the help center, please edit the question.

1

I found out ! It was just a syntax error when inserting the expression that got back the attribute value from the expression builder tool. One needs to add "" around the expression.

from os import startfile
from os.path import abspath, dirname, join, isfile
from PyQt4.QtGui import QMessageBox
proj = QgsProject.instance()
urfile = str(proj.fileName())
path = join(abspath(dirname(urfile)),"Fichiers_Coupes_SCAN", "[% "ID_SONDAGE" %]"".jpg")
import os.path
if os.path.isfile(path) :
    startfile(path)
else:
    QMessageBox.information(None, "Erreur : ","[%'The crosscut from the borehole  '  ||  "ID_SONDAGE"  || ' can't be found'%]")

Not the answer you're looking for? Browse other questions tagged or ask your own question.