4

I am trying to create a standalone script using the code below. I was able to add a shapefile with no errors but when I add map = QgsMapCanvas() after qgs.initQgis() and map.setLayerSet([QgsMapCanvasLayer(layer)]) after QgsMapLayerRegistry.instance().addMapLayer(layer) I get the following errors: Cannot Create a QPixmap when no GUI is being used and Cannot Create a Qwidget when no GUI is being used

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

import sys, os

qgs = QgsApplication(sys.argv, False)
qgis_prefix = "C:\\OSGeo4W\\apps\\qgis"
QgsApplication.setPrefixPath(qgis_prefix, True)
qgs.initQgis()

map = QgsMapCanvas()

layer = QgsVectorLayer("F:/IrrigatedLands/FC_qgis/boundary.shp", "testlayer_shp", "ogr")

if not layer.isValid():
    print "Layer failed to load!"

else:
    print "Layer was loaded successfully!"

QgsMapLayerRegistry.instance().addMapLayer(layer)

map.setLayerSet([QgsMapCanvasLayer(layer)])

print layer
4

The second parameter of the QgsApplication enables/disables the QGIS GUI components in your script. Just change it to true:

qgs = QgsApplication(sys.argv, True)

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.