0

I try to open the attribute table of a layer witch has a PythonInit fonction with a Ui form and returns "AttributeError: 'NoneType' object has no attribute 'length'". Made so far:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

amonte = None
aval = None
panta = None
layer = None
lungimeTextEdit = None
ValLungime = None
valoare = None
feature = None

def formOpen(dialog,layer,feature):
   global myDialog
   myDialog = dialog
   global lungimeTextEdit
   global amonte
   global aval
   global panta

   lungimeTextEdit = dialog.findChild(QLineEdit,"shape_leng")
   amonte = dialog.findChild(QLineEdit, "amonte")
   aval = dialog.findChild(QLineEdit,"aval" )
   panta = dialog.findChild(QLineEdit, "pente")
   buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")
   lungimeTextEdit.setText(str(feature.geometry().length()))
   buttonBox.accepted.disconnect(myDialog.accept)

enter image description here

1

The error is thrown because you try to call the length() function for an object which doesn't exist. It could either be that the geometry() function of the current feature does not return anything or that the feature itself doesn't exist. Since you initialize the feature variable with a value of None it seems that no new value is assigned to the variable. You should ensure that a feature object is handed over to the formOpen function.

  • I just want to open the attribute table of the layer, and returns this error; so there is not on a feature creating. Any ideas ?? – G3orge Mar 30 '16 at 12:24
  • I also removed: layer = None, feature = None – G3orge Mar 30 '16 at 12:34
1

Solution:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
amonte = None
aval = None
panta = None
ValLungime = None
lungimeTextEdit = None

def formOpen(dialog,layer,feature):
    global myDialog
    myDialog = dialog
    global amonte
    global aval
    global panta

if feature.geometry() is None:
    return

lungimeTextEdit = dialog.findChild(QLineEdit,"shape_leng")
amonte = dialog.findChild(QLineEdit, "amonte")
aval = dialog.findChild(QLineEdit,"aval" )
panta = dialog.findChild(QLineEdit, "pente")
buttonBox = dialog.findChild(QDialogButtonBox,"buttonBox")
lungimeTextEdit.setText(str(feature.geometry().length()))

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.