[Qgis-developer] problem with forms and python init function

Nathan Woodrow madmanwoo at gmail.com
Fri Dec 20 03:19:57 PST 2013


Hey Luca,

If all you want to do is load the attributes for a column from layer you
can just do this:

layer = QgsMapLayerRegistry.instance().mapLayersByName('yourname')[0]

for feature in layer.getFeatures():
     combo.addItem(feature['columnname'])

That will loop all the features and load the value from columnname into the
combo.  You will have to add more smarts to that if you need more logic.

If you are in your initFunction you already have the layer:

def init(dialog, feature, layer):
  for feature in layer.getFeatures():
        .....

Nathan


On Fri, Dec 20, 2013 at 10:03 PM, Luca Lanteri <mescal72 at gmail.com> wrote:

> Hi to all,
>
> I'm a newbie with pyqgis so apologize me for the trivial question. I'm
> trying to create a forms using QT and to add some intelligence into it
> using the "python Init function", present in the Layer properties ->
> field menu. I want to update the data in combo2 on the base of the
> combo1 choosed value, but I encountered some problem, probably due to
> my lack of knowled of QGIS API. This is my first problems:
>
> I try to read some data from a layer using this code [1]. If the layer
> is a geometry layer all works fine but if I try to read the data from
> a table (without geom coloumn) i cant read the attributes. Both object
> are QgsVectorLayer so I though that I can use the same methods. In
> order to resolve this problem i just addes a geom coloum to my table
> but it's just a ploy.
>
> The second, and the very real problem, is that when I try to update
> the the value using the
> curLayer.changeAttributeValue() method some strange beaviours occours.
> - If i try to commit changes from the code (using
> curLayer.commitChanges() ), and I'm in the table form view, Qgis
> crash.
> - when I try to commit changes use the toogle editing mode button, and
> I'm in the table form view, all my changes are lost.
> - when I try to commit changes use the toogle editing mode button, and
> I'm just in the form view, changes are lost alternatively once on two
> saving operation.
>
> I don't understand if this is a problem of my code or a qgis bug.
> Any hint ?
>
> I hope to have described the problem clearly.
> thank in advance
>
> Luca
>
>
> [1]
> alegend = iface.legendInterface()
> allLayers=alegend.layers()
>     for i in allLayers:
>         if i.name()=='mytab':
>             aLayer=i
> lista=[]
> rsTipo=[]
> for i in afeature:
>         a=i.attributes()
>         l1=i.attributes()[0]
>         l2=i.attributes()[1]
>         l3=i.attributes()[2]
>         l4=i.attributes()[3]
>         alist=[l1,l2,l3,l4]
>         rsTipo.append(alist)
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20131220/f3f90db7/attachment.html>


More information about the Qgis-developer mailing list