Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using QgsComposition for the first time, but I fail at the first step :-(

mapRenderer = iface.mapCanvas().mapRenderer()

line 31, in mapRenderer = iface.mapCanvas().mapRenderer()

AttributeError: 'NoneType' object has no attribute 'mapCanvas'

Thanks for help or any advice.

PS: The Script

from PyQt4 import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from glob import glob
from qgis.gui import *
from qgis.core import *
from qgis.utils import*

QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis/", True)
QgsApplication.initQgis()

name="test.tiff"
fileName = name
fileInfo = QFileInfo(fileName)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(fileName, baseName)

QgsMapLayerRegistry.instance().addMapLayer(rlayer)

mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
share|improve this question
    
Where is iface initialized? –  whuber Jul 2 '12 at 14:56
1  
>>> from qgis.utils import * >>> iface <qgis.gui.QgisInterface object at 0x11f1acb00> –  dakcarto Jul 2 '12 at 18:23
add comment

2 Answers 2

The qgis.utils.iface (QgisInterface class) object is an...

"Abstract base class defining interfaces exposed by QgisApp and made available to plugins"

Basically, that interface is available to attached scripts (e.g. plugins) while QGIS app is running. I'm not an expert on this, but it looks like you are using the QGIS Python bindings to directly interact with the API, by way of an external PyQt4 app. There is no iface object in this case, since QGIS is running as a library to the app you are building, and QGIS app's main GUI has not been initialized and shown.

You will need to build your own PyQt4 app and instantiate components of the QGIS API with the interface widgets of your app as parents to those objects. There are some dated but still valid examples on the older QGIS dev blog that Martin Dobias ported over from C++. Those tutorial files have full examples of running an external app and leveraging QGIS as a GIS/GUI library.

share|improve this answer
add comment

Like dakcarto said there is no iface instance when working with a PyQt4 app built using the QGIS API. You will need to construct your own map canvas object.

canvas = QgsMapCanvas()
canvas.mapRenderer()

In order to add your layers you will need to use the setLayerSet method

canvas.setLayerSet([rlayer])
share|improve this answer
    
Ok realy thx for fast help. –  grorol Jul 3 '12 at 5:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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