[Qgis-user] Add a grid to a map frame

Charles Dixon-Paver charles at kartoza.com
Mon Apr 12 12:21:38 PDT 2021


Try use map.grids().addGrid(map_grid) instead of map_grids =
 QgsLayoutItemMapGridStack(map).addGrid(map_grid) and check your results?


On Mon, 12 Apr 2021 at 20:11, Mayo Rubiano <mayo.rubianoh at gmail.com> wrote:

> Hello everyone. I was trying to layout with pyQGIS, and I already have the
> map frame with the layers, but now I need to add a grid, I tried with the
> code written down, but the grid didn't work. I'm new pyQGIS and maybe there
> are errors in the code.
>
> project = QgsProject().instance()
> layer_name = "Curvas de nivel"
> layout_name = "MyLayout"
> layout_page = ["Letter", 0]
> layers = QgsProject.instance().mapLayersByName(layer_name)[0]
>
> for layout in project.layoutManager().printLayouts():
>     if layout.name() == layout_name:
>        project.layoutManager().removeLayout(layout)
>
> layout = QgsPrintLayout(project)
> page = QgsLayoutItemPage(layout)
> page.setPageSize(layout_page[0], layout_page[1])
> layout.pageCollection().addPage(page)
> map = QgsLayoutItemMap(layout)
> map.attemptMove(QgsLayoutPoint(5,5,0))
> map.attemptResize(QgsLayoutSize(200,200,0))
> map.setExtent(layers.extent())
>
> map_grid =  QgsLayoutItemMapGrid("Grid", map)
> map_grid.setEnabled(True)
> map_grid.setIntervalX(layers.extent().width() / 3)
> map_grid.setIntervalY(layers.extent().width() / 3)
> map_grids =  QgsLayoutItemMapGridStack(map).addGrid(map_grid)
> layout.addLayoutItem(map)
>
>
> layout.setName(layout_name)
> project.layoutManager().addLayout(layout)
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210412/a5de0d3d/attachment.html>


More information about the Qgis-user mailing list