[Qgis-user] Python form logic on attribute table open
Thomas Colley
it087 at neath-porttalbot.gov.uk
Tue May 3 07:49:58 PDT 2016
Hi Matthias
Thanks for the explanation, I think I am using globals where I shouldn’t be. In this case I don’t think it’s the global variable causing the problem though.
I’ve stripped everything back to a really simple example (no global variables). I have a custom UI with one QLineEdit called parish.
On opening the form Python performs an intersect with the feature against a polygon layer of parishes and uses the parish name to set the text in the QLineEdit.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
def my_form_open(dialog, layer, feature):
geom = feature.geometry()
parishField = dialog.findChild(QLineEdit, "parish")
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == 'parishes':
for f in lyr.getFeatures():
if geom.intersects(f.geometry()):
parishField.setText(f.attribute('name'))
break
break
This works fine using the identify tool but throws up the following error when opening the attribute table:
An error occurred during execution of following code:
my_form_open( _qgis_featureform_22_16, _qgis_layer_test_lines20160503150512486_16, _qgis_feature_20160503154237941)
Traceback (most recent call last):
File "", line 1, in
File "", line 23, in my_form_open
AttributeError: 'NoneType' object has no attribute 'intersects'
I can see why the error is happening: When opening the attribute table the feature is passed without geometry so the intersect fails, but for my purposes I don’t want it to be running the Python code anyway as I don’t want it calculating fields unless I’ve actually clicked on the feature. Opening the attribute table just runs it against the first record in the table.
That’s why I was hoping there’d be some way of detecting the context of the form. QgsAttributeEditorContext has a FormMode that returns Embed, StandaloneDialog or Popup, so I thought that looked promising but I can’t work out how to access it.
Thanks
Tom
From: Matthias Kuhn [mailto:matthias at opengis.ch]
Sent: 03 May 2016 13:52
To: Thomas Colley; qgis-user at lists.osgeo.org
Subject: Re: [Qgis-user] Python form logic on attribute table open
Hi Tom,
Globals are really for "there will only ever be one of these" objects. And that's not true for feature forms.
You will have the same kind of trouble if the user opens the feature form of two features in parallel because they will work on the same global variable (which will be overwritten and may even be deleted by form A whil form B is still open).
I assume you have a slot connected to some widget's changed signal and need the reference to parameters inside the slot. It's best to pass them as parameters to the slot.
Something like:
----------
import functools
def my_slot( my_form, new_text ):
do_something_with(my_control)
print new_text
def form_init(form, y, z):
control.textChanged.connect(functools.partial(my_slot, form))
----------
On 05/03/2016 01:50 PM, Thomas Colley wrote:
Hi Matthias
I am using global variables. In what way do they cause problems?
Is it expected behaviour for the Python logic to be used when the attribute table is opened?
I know what is causing the Python error. I am performing an intersect query to populate an attribute. When I open the attribute table the first feature in the attribute table is being passed to the Python form object but it doesn’t include the geometry. This gives AttributeError: 'NoneType' object has no attribute 'intersects'.
I have no need for any of the Python logic on opening of the attribute table, although I can see the advantage if it was open in form view.
Hope this makes sense, if you have any more details on the global variables I’d be grateful.
Thanks
Tom
From: Qgis-user [mailto:qgis-user-bounces at lists.osgeo.org] On Behalf Of Matthias Kuhn
Sent: 03 May 2016 09:45
To: qgis-user at lists.osgeo.org<mailto:qgis-user at lists.osgeo.org>
Subject: Re: [Qgis-user] Python form logic on attribute table open
Hi Tom,
Are you using global variables? They often lead to troubles in this context.
Matthias
On 05/03/2016 10:42 AM, Thomas Colley wrote:
Hi
I have a Python init function bound to an attribute form. This is working great using the identify tool to open a feature form but is throwing an error when I open the layer’s attribute table.
Is there any way I can check in the Python whether a feature form has been opened or the attribute table. I have no need for the logic when opening the attribute table.
I’ve found QgsAttributeEditorContext that looks like it might do the trick but I can’t work out how to access it.
Any advice would be appreciated.
Thanks
Tom
_______________________________________________
Qgis-user mailing list
Qgis-user at lists.osgeo.org<mailto:Qgis-user at lists.osgeo.org>
List info: http://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-user
--
Matthias Kuhn
OPENGIS.ch - https://www.opengis.ch
Spatial • (Q)GIS • PostGIS • Open Source
--
Matthias Kuhn
OPENGIS.ch - https://www.opengis.ch
Spatial • (Q)GIS • PostGIS • Open Source
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20160503/b1360f96/attachment.html>
More information about the Qgis-user
mailing list