1

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'

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)
2
  • Where is iface initialized?
    – whuber
    Jul 2, 2012 at 14:56
  • 1
    >>> from qgis.utils import * >>> iface <qgis.gui.QgisInterface object at 0x11f1acb00>
    – dakcarto
    Jul 2, 2012 at 18:23

2 Answers 2

3

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.

1

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])
1
  • Ok realy thx for fast help.
    – grorol
    Jul 3, 2012 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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