1

I'm working on project where I need insert QGIS canvas as a widget in QGIS plugin. When user click on button it should be loaded into widget. Something like this, this is example where satellite map should be loaded in widget: enter image description here

How to implement this in python code?

1

In QT designer you need make right click on the widget and select "promote to", and write the class and header as seen in the image.

enter image description here

after you need compile the .ui.

In the class that loads this ui, to load some layer for example (this canvas have a objectname "canvas")

def __init__(self):
    """ Contructor """
    QDialog.__init__(self)
    self.setupUi(self)
    # Load layers
    self.airports = QgsVectorLayer(r"C:\Users\fran\Downloads\qgis_sample_data\qgis_sample_data\shapefiles\airports.shp", " airports ", "ogr")
    self.canvas.setLayers([self.airports])
    self.canvas.setExtent(self.airports.extent())
    self.canvas.refresh()
    self.canvas.waitWhileRendering()
    # Extra : set pan tool in this canvas
    self.tool = QgsMapToolPan(self.canvas)
    self.canvas.setMapTool(self.tool)

Result

enter image description here

  • what about raster layer? Is it the same way? – Ivan May 10 '19 at 12:08
  • Nope, you need use QgsRasterLayer – Fran Raga May 10 '19 at 12:18
  • Is it possible to implement it in plugin that is already done. – Ivan May 10 '19 at 12:29
  • UI file is compiled and all the logic is done. Now I only need to add canvas widget – Ivan May 10 '19 at 12:32
  • for load raster you have this gis.stackexchange.com/questions/144058/… – Fran Raga May 10 '19 at 12:47

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