0

I'm unsure if i'm properly using the processing.getObject() function. I'm creating a python processing script for QGIS 2.0 and 2.2, but can't seem to get an attribute table to open programmatically. The code below works fine in the Python Console, but causes QGIS to crash when I tried as a Processing Script. The only difference was that the layer is called by QgsVectorLayer() in the Python Console and processing.getObject() in the Processing script, thus my reasoning behind my question.

from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from processing.core.VectorWriter import VectorWriter
import numpy
import processing
import csv
import qgis.utils

#Here we define the input and outputs
#====================================
##[Example scripts]=group
##Couche_compteurs_individuels=vector
##Couche_compteurs_generaux=vector
##Couche_canalisations_sur_compteur=vector

#And here is the body of the algorithm
#=======================================

#input layers values are always a string with its location.
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
#using the processing.getobject() method
indivLayer = processing.getobject(Couche_compteurs_individuels)
grosLayer = processing.getobject(Couche_compteurs_generaux)
resLayer = processing.getobject(Couche_canalisations_sur_compteur)

CG_IDlist = []

def AEPstats():
    Gros_Feat = grosLayer.getFeatures()
    grosIDselection = []
    grosidNull = False
    for feat in Gros_Feat:
        ID_idx = grosLayer.fieldNameIndex('ID_CG')
        #selection list of null values
        if feat.attributes()[ID_idx] == NULL:
            grosIDselection.append(feat.id())
            grosLayer.setSelectedFeatures(grosIDselection)
            grosidNull = True
        #Putting existing attributes in list
        else:
            CG_IDlist.append(feat.attributes()[ID_idx])
    #Open msg box, attribute table with selected items, and end function
    if grosidNull == True:
        QMessageBox.warning(
            None,
            'Probleme de saisie',
            "Completez identifiant(s) de reference ('CG_ID') de Compteurs Individuel")
        qgis.utils.iface.showAttributeTable(grosLayer)
        return

Thanks in advance

  • I don't think showing the attribute table in a processing script is a good idea. Makes it hard to add it to a model if it does other stuff in the UI. – Nathan W Apr 2 '14 at 21:20
  • I'm in the process of creating a plugin, but wanted a quick way to implement a user dialog as I'm pretty inexperienced with PyQt and can tell this is going to take me a while to get going. – user25976 Apr 3 '14 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.