Re: [QGIS-Developer] PyQGIS code: setting filter expression for value relation widget

2023-02-24 Thread Andreas Neumann via QGIS-Developer
Hi Germán,

Thank you so much for these suggestions and code snippets.

I also came across the QgsEditorWidgetSetup - but had no idea how to use
it. The Python cookbook did not mention it. The cookbook is really great -
but for interactive forms, there is not so much information.

I will try it and report back.

Thank you very much Germán,
Andreas


On Sun, 19 Feb 2023 at 00:59, Germán Carrillo via QGIS-Developer <
qgis-developer@lists.osgeo.org> wrote:

> Hi Andreas,
>
>
> you can modify your Value Relation widget's filter expression using the
> QgsEditorWidgetSetup class, in this way:
>
> 1) Get the editor widget setup and get a copy of its config dictionary:
>
> ews = layer.editorWidgetSetup(3)  # *Let's assume your Value Relation
> widget is set for a field with index 3*
> new_config = ews.config()  # *We'll use the current config as a basis
> to modify only the filter expression*
>
> 2) Modify the config dict's filter expression:
>
> new_config['FilterExpression'] = 'your new expression'
>
> 3) Apply changes to your layer:
>
> new_ews = QgsEditorWidgetSetup('ValueRelation', new_config)
> layer.setEditorWidgetSetup(3, new_ews)  # *Remember that 3
> corresponds to the field index*
>
> And that's it! You should have now a different set of values in the Value
> Relation widget.
>
>
> Regards,
>
> Germán
>
>
>
> El lun, 13 feb 2023 a las 5:40, Andreas Neumann via QGIS-Developer (<
> qgis-developer@lists.osgeo.org>) escribió:
>
>> Hi,
>>
>> I am looking for a code snippet in PyQGIS: how can I change the filter
>> expression for a value relation widget?
>>
>> Technically, the widget seems to be a QComboBox. I can get access to the
>> combobox, e.g. to get the current value - but how can I access the QGIS
>> related functionality, like changing programmatically settings of the value
>> relation widget , such as the filter expression? I found the
>> QgsValueRelationFieldFormatter object, but it doesn't allow me to change
>> the filter expression. Any idea how I would change the filter expression
>> using Python?
>>
>> Thank you for any hints,
>>
>> Andreas
>> ___
>> QGIS-Developer mailing list
>> QGIS-Developer@lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>
> --
> ---
>|\__
> (:>__)(
>|/
> Soluciones Geoinformáticas Libres
> http://geotux.tuxfamily.org/
> https://twitter.com/GeoTux2 
>
> 
> ___
> QGIS-Developer mailing list
> QGIS-Developer@lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>


-- 

--
Andreas Neumann
QGIS.ORG board member (treasurer)
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [QGIS-Developer] PyQGIS code: setting filter expression for value relation widget

2023-02-18 Thread Germán Carrillo via QGIS-Developer
Hi Andreas,


you can modify your Value Relation widget's filter expression using the
QgsEditorWidgetSetup class, in this way:

1) Get the editor widget setup and get a copy of its config dictionary:

ews = layer.editorWidgetSetup(3)  # *Let's assume your Value Relation
widget is set for a field with index 3*
new_config = ews.config()  # *We'll use the current config as a basis
to modify only the filter expression*

2) Modify the config dict's filter expression:

new_config['FilterExpression'] = 'your new expression'

3) Apply changes to your layer:

new_ews = QgsEditorWidgetSetup('ValueRelation', new_config)
layer.setEditorWidgetSetup(3, new_ews)  # *Remember that 3 corresponds
to the field index*

And that's it! You should have now a different set of values in the Value
Relation widget.


Regards,

Germán



El lun, 13 feb 2023 a las 5:40, Andreas Neumann via QGIS-Developer (<
qgis-developer@lists.osgeo.org>) escribió:

> Hi,
>
> I am looking for a code snippet in PyQGIS: how can I change the filter
> expression for a value relation widget?
>
> Technically, the widget seems to be a QComboBox. I can get access to the
> combobox, e.g. to get the current value - but how can I access the QGIS
> related functionality, like changing programmatically settings of the value
> relation widget , such as the filter expression? I found the
> QgsValueRelationFieldFormatter object, but it doesn't allow me to change
> the filter expression. Any idea how I would change the filter expression
> using Python?
>
> Thank you for any hints,
>
> Andreas
> ___
> QGIS-Developer mailing list
> QGIS-Developer@lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>

-- 
---
   |\__
(:>__)(
   |/
Soluciones Geoinformáticas Libres
http://geotux.tuxfamily.org/
https://twitter.com/GeoTux2 


___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [QGIS-Developer] PyQGIS code: setting filter expression for value relation widget

2023-02-13 Thread Andreas Neumann via QGIS-Developer

Hi,

I found the "QgsValueRelationSearchWidgetWrapper" class, which seems to 
have a "setExpression" method - but I don't know how I can access and 
change the QgsValueRelationSearchWidgetWrapper of an existing ComboBox 
in the attribute form.


Andreas

On 2023-02-13 11:31, Andreas Neumann via QGIS-Developer wrote:


Hi,

I am looking for a code snippet in PyQGIS: how can I change the filter 
expression for a value relation widget?


Technically, the widget seems to be a QComboBox. I can get access to 
the combobox, e.g. to get the current value - but how can I access the 
QGIS related functionality, like changing programmatically settings of 
the value relation widget , such as the filter expression? I found the 
QgsValueRelationFieldFormatter object, but it doesn't allow me to 
change the filter expression. Any idea how I would change the filter 
expression using Python?


Thank you for any hints,

Andreas
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer


[QGIS-Developer] PyQGIS code: setting filter expression for value relation widget

2023-02-13 Thread Andreas Neumann via QGIS-Developer

Hi,

I am looking for a code snippet in PyQGIS: how can I change the filter 
expression for a value relation widget?


Technically, the widget seems to be a QComboBox. I can get access to the 
combobox, e.g. to get the current value - but how can I access the QGIS 
related functionality, like changing programmatically settings of the 
value relation widget , such as the filter expression? I found the 
QgsValueRelationFieldFormatter object, but it doesn't allow me to change 
the filter expression. Any idea how I would change the filter expression 
using Python?


Thank you for any hints,

Andreas___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer