1

I am trying to show the shapefile in canvas using standalone application. However it does not show the content. Here is my code and I am using PyQt5, Qgis 2.99, PyCharm 3.6

from qgis.core import *
from qgis.gui import *
from qgis.utils import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

app = QgsApplication([], True)
path = "C:/Users/DELL/AppData/Roaming/QGIS"
app.setPrefixPath(path, True)
app.initQgis()
canvas = QgsMapCanvas()
title = "PyQGIS Standalone Application Example"
canvas.setWindowTitle(title)
canvas.setCanvasColor(Qt.white)
layer_info = "C:/Users/DELL/Desktop/GIS_CensusTract_poly.shp"
layer = QgsVectorLayer(layer_info, 'MyShp', "ogr")
layer.updateExtents()
QgsProject.instance().addMapLayer(layer)
canvas.setExtent(layer.extent())
canvas.setLayers([layer])
canvas.zoomToFullExtent()
canvas.show()
exitcode = app.exec_()
QgsApplication.exitQgis()
sys.exit(exitcode)
  • it's recomanded each time you load a file, to verify if the layer is valid with layer.isValid() method, to prevent any problem with you dataset or the path... – Hugo Roussaffa - GeoDatup Oct 8 '17 at 19:12
1

Most times if your prefix path is wrong, the map fails to display. Try changing your paths to

qgis_prefix="C:\\Program Files\\QGIS\\apps\\qgis"  
                    OR
qgis_prefix="C:/OSGeo4W64/apps/qgis"              # if running from OSGeo4W

QgsApplication.setPrefixPath(qgis_prefix, 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.