4

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?

Ivan
  • 145
  • 6

1 Answers1

4

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

Fran Raga
  • 7,838
  • 3
  • 26
  • 47