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
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
brgionta
  • 507
  • 5
  • 14

1 Answers1

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)
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
  • LOL this answers the comment I had on another answer of yours at: http://gis.stackexchange.com/questions/129513/how-can-i-access-processing-with-python/130337#comment281594_130337 – Mr Purple Apr 08 '16 at 07:02
  • That's hilarious! Glad to know your question was solved (I had no idea). Could you please answer your comment there for future readers? – Germán Carrillo Apr 08 '16 at 17:28