Thursday, 13 September 2018

Qgis Python console print statements not working


I have a small problem with the Python console in Qgis. I am working through some introductory tutorials on Python scripting for Qgis. I have a script that loads layers, then queries the canvas for names and loops through the names and prints them, something like this:


print "This print statement does not help"
#

# import layers to canvas
qgis.utils.iface.addVectorLayer("/path/file1.shp", "First file", "ogr")
qgis.utils.iface.addVectorLayer("/path/file2.shp", "Second file", "ogr")
qgis.utils.iface.addRasterLayer("/path/file3.tif", "Third file")
#
# print "This line triggers the print function in the for loop to work"
#
canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
#

for i in allLayers: print i.name()

The problem is that, unless I print to the console before the for loop but after the imports, the layer names are not printed, the script simply exists cleanly. If I run the script a second time (with the files still in the canvas) the files are imported again and the for loop works, but of course now there are two copies of the files in the canvas. Is this yet another path problem or a bug or am I expecting too much?


(OSX 10.7.5 and Qgis 2.6.0)




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...