[Qgis-user] Python form logic on attribute table open

Matthias Kuhn matthias at opengis.ch
Tue May 3 05:51:57 PDT 2016


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
> *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/ef1296dd/attachment.html>


More information about the Qgis-user mailing list