<div dir="ltr">Hey Stephane,<div><br></div><div>This is because your standalone script doesn't use the new SIP API.  I pushed a change last night so that now importing qgis will set the API to version to.  All you need to do is make sure you import it first before any of the PyQt4 modules are.</div>

<div><br></div><div>Before:</div><div>from PyQt4.QtCore import *</div><div>from qgis.core import *</div><div><br></div><div>After</div><div><div>from qgis.core import *<br></div><div>from PyQt4.QtCore import *</div><div>
<br>
</div></div><div><br></div><div>If you import PyQt4 first it will set the API to version 1 which you can't change after.</div><div><br></div><div>- Nathan</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">

On Thu, Jul 18, 2013 at 5:13 AM, Stéphane Henriod <span dir="ltr"><<a href="mailto:s@henriod.info" target="_blank">s@henriod.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="ltr"><div><div><div><div><div>Dear all<br><br></div>I am trying to get into the new 2.0 API and am still facing issues for very basic stuff: getting the values of the attributes out of a PostGIS layer.<br><br></div>


Basically, it works (with the almost same code) in the Qgis python console, but I don't manage to get it to work in a stand-alone script.<br><br></div>What I have so far is a PostGIS layer, that I load with:<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">


<i>uri = QgsDataSourceURI()<br>uri.setConnection('localhost', '5432', 'db_name', 'user_name', 'password')<br>uri.setDataSource('my_schema', 'my_table', 'the_geom')<br>


    <br>my_layer = QgsVectorLayer(uri.uri(),'myhospitals','postgres')<br></i></blockquote><br></div>A quick test shows me that the layer has been successfully loaded:<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">


<i>if not my_layer.isValid():<br>    print "Layer failed to load!"<br>else:<br>    print "Layer loaded successfully"<br></i></blockquote><br></div>I would assume that<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">


<i>for elem in my_layer.getFeatures():</i><br><i>    print elem.attributes()</i><br></blockquote><div><div><div><br></div><div>should return the attributes in a usable format but, instead, I get:<br><i><br></i><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">


<i>[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant object at 0xad37acdc>, None, <PyQt4.QtCore.QVariant object at 0xad37ad14>]</i><br><i>[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant object at 0xad37acdc>, <PyQt4.QtCore.QVariant object at 0xad37ad14>, <PyQt4.QtCore.QVariant object at 0xad37ad4c>]</i><br>


<i>[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant object at 0xad37acdc>, <PyQt4.QtCore.QVariant object at 0xad37ad14>, <PyQt4.QtCore.QVariant object at 0xad37ad4c>]</i><br><i>[<PyQt4.QtCore.QVariant object at 0xad37aca4>, <PyQt4.QtCore.QVariant object at 0xad37acdc>, <PyQt4.QtCore.QVariant object at 0xad37ad14>, <PyQt4.QtCore.QVariant object at 0xad37ad4c>]</i><br>


</blockquote></div><div><br></div><div>Does anyone have an idea where the problem could lie?<br><br></div><div>For those who speak French, the question is posted here as well: <a href="http://www.forumsig.org/showthread.php/37178-Nouvelle-API-r%C3%A9cup%C3%A9rer-les-valeurs-attributaires" target="_blank">http://www.forumsig.org/showthread.php/37178-Nouvelle-API-r%C3%A9cup%C3%A9rer-les-valeurs-attributaires</a><br>


</div><div><br></div><div>I am using Qgis 1.9.0 with Python 2.7 on Ubuntu 13.04<br><br></div><div>Thanks in advance for ideas and comments...<br><br>Cheers<br><br>Stéphane<br clear="all"></div><div><div><div><div><div><div>


--<br>"Le mot progrès n'aura aucun sens tant qu'il y aura des enfants malheureux" -- Albert Einstein<br><br>"A journey does not need reasons. Before long, it proves to be reason enough in itself. One thinks that one is going to make a journey, yet soon it is the journey that makes or unmakes you." -- Nicolas Bouvier<br>


<br>Photos de voyages, photos de montagne: <a href="http://www.henriod.info/" target="_blank">http://www.henriod.info</a><span dir="ltr"><span dir="ltr" title="Appeler ce numéro de téléphone en/au(x) Tadjikistan avec Skype : +992934624662"><span>  </span></span> <br>


</span></div>
</div></div></div></div></div></div></div></div>
<br>_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
<br></blockquote></div><br></div>