3

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)

4

The best way to print out logging in qgis is to use the QgsMessageLog class. This writes to the debug log window rather then the console.

QgsMessageLog.logMessage("message", "name")

In the lower right of the main window you will see a icon that you can press to open the debug log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.