1

I am constructing a GUI using PyQT4 and QGis. Rigth now I can open the MapCanvas whith same buttons of Zoom In/Out using this code:

from qgis.gui import *
from qgis.core import *
from qgis.PyQt.QtGui import QAction
from qgis.PyQt.QtCore import SIGNAL, Qt
from qgis.PyQt.QtWidgets import QMainWindow

class MyWnd(QMainWindow):
    def __init__(self, layer):
        QMainWindow.__init__(self)

        self.canvas = QgsMapCanvas()
        self.canvas.setCanvasColor(Qt.white)

        self.canvas.setExtent(layer.extent())
        self.canvas.setLayerSet([QgsMapCanvasLayer(layer)])
        #self.canvas.setLayers([layer])


        self.setCentralWidget(self.canvas)

        self.actionZoomIn = QAction("Zoom in", self)
        self.actionZoomOut = QAction("Zoom out", self)
        self.actionPan = QAction("Pan", self)

        self.actionZoomIn.setCheckable(True)
        self.actionZoomOut.setCheckable(True)
        self.actionPan.setCheckable(True)

        self.actionZoomIn.triggered.connect(self.zoomIn)
        self.actionZoomOut.triggered.connect(self.zoomOut)
        self.actionPan.triggered.connect(self.pan)

        self.toolbar = self.addToolBar("Canvas actions")
        self.toolbar.addAction(self.actionZoomIn)
        self.toolbar.addAction(self.actionZoomOut)
        self.toolbar.addAction(self.actionPan)

        # create the map toolsq
        self.toolPan = QgsMapToolPan(self.canvas)
        self.toolPan.setAction(self.actionPan)
        self.toolZoomIn = QgsMapToolZoom(self.canvas, False) # false = in
        self.toolZoomIn.setAction(self.actionZoomIn)
        self.toolZoomOut = QgsMapToolZoom(self.canvas, True) # true = out
        self.toolZoomOut.setAction(self.actionZoomOut)

        self.pan()

    def zoomIn(self):
        self.canvas.setMapTool(self.toolZoomIn)

    def zoomOut(self):
        self.canvas.setMapTool(self.toolZoomOut)

    def pan(self):
        self.canvas.setMapTool(self.toolPan)



`Teste Application:
QgsApplication.setPrefixPath(r"C:\Program Files (x86)\QGIS 2.18", True)

qgs = QgsApplication([], True)
qgs.initQgis()

layer=QgsVectorLayer('C:/ProgramFiles(x86)/QGIS2.18/gis_data/PRT_water_areas_dcw.shp', 'water','org')

w = MyWnd(layer)
w.show()

qgs.exec_()
qgs.exitQgis()`

However, I am getting a blank MapCanvas with nothing on it that crushes my program. I think that it is related to this line:

self.canvas.setLayerSet([QgsMapCanvasLayer(layer)])

But I cant understand why this is happening.

closed as too broad by Fran Raga, Jochen Schwarze, whyzar, xunilk, HDunn May 19 at 9:04

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.