Bug report #19334

Why python function not work in custom function in QGIS 3.0?

Added by dany Lefeuvre about 2 years ago. Updated almost 2 years ago.

Status:Open
Priority:Normal
Assignee:-
Category:Unknown
Affected QGIS version:3.2 Regression?:No
Operating System:Windows 7 Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:27162

Description

in QGIS atlas I use a custom function to find the first file containing texts :

import os, fnmatch
from PyQt5.QtCore import QFileInfo
from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def findphoto(nb, src1,src2,feature, parent):
    lnb=0
    val='NA'
    filen = QFileInfo(QgsProject.instance().fileName())
    Dir_report=filen.absolutePath()
    for root, directories, filenames in os.walk(Dir_report):
        for filename in filenames: 
            if fnmatch.fnmatch(filename, '*' +str(src1) + '*' + str(src2) + '*.jpg' ):
                lnb=lnb+1
                if lnb==nb:
                    val=os.path.join(root,filename)
                    break
    return val

In QGIS 2.18 it works fine (just change PyQt5 by PyQt4), in QGIS 3.2 it return "NA" value.

In QGIS 3.2 if I try in python mode, it works fine:

import os, fnmatch
from PyQt5.QtCore import QFileInfo
from qgis.core import *
from qgis.gui import *

def findphoto(nb, src1,src2):
    lnb=0
    val='NA'
    filen = QFileInfo(QgsProject.instance().fileName())
    Dir_report=filen.absolutePath()
    for root, directories, filenames in os.walk(Dir_report):
        for filename in filenames: 
            if fnmatch.fnmatch(filename, '*' +str(src1) + '*' + str(src2) + '*.jpg' ):
                lnb=lnb+1
                if lnb==nb:
                    val=os.path.join(root,filename)
                    break
    return val

why?

History

#1 Updated by dany Lefeuvre about 2 years ago

oups error in tittle this is for qgis 3.2

#2 Updated by Jürgen Fischer almost 2 years ago

  • Description updated (diff)

Also available in: Atom PDF