0

I'm trying to implement a scalebar for my layout in a standalone application.

This is my current code:

def createLayout(Main):
    layers = Main.canvas.layers()
    project = QgsProject.instance()
    manager = project.layoutManager()
    layoutName = 'Layout1'
    print(project.mapLayers())
    layouts_list = manager.printLayouts()
    for layout in layouts_list:
        if layout.name() == layoutName:
            manager.removeLayout(layout)
    layout = QgsPrintLayout(project)
    layout.initializeDefaults()
    layout.setName(layoutName)
    manager.addLayout(layout)

    map = QgsLayoutItemMap(layout)
    map.setRect(20, 20, 20, 20)
    map.setLayers(layers)

    ms = QgsMapSettings()
    ms.setLayers(layers)
    rect = QgsRectangle(ms.fullExtent())
    rect.scale(1.0)
    ms.setExtent(rect)
    map.setExtent(rect)
    map.setBackgroundColor(QColor(255, 255, 255, 0))
    layout.addLayoutItem(map)

    map.attemptMove(QgsLayoutPoint(0, 0, QgsUnitTypes.LayoutMillimeters))
    map.attemptResize(QgsLayoutSize(297, 210, QgsUnitTypes.LayoutMillimeters))

    # Scalebar
    scalebar = QgsLayoutItemScaleBar(layout)
    scalebar.setStyle('Line Ticks Up')
    scalebar.setUnits(QgsUnitTypes.DistanceKilometers)
    scalebar.setNumberOfSegments(2)
    scalebar.setNumberOfSegmentsLeft(0)
    scalebar.setUnitsPerSegment(0.5)
    scalebar.setLinkedMap(map)
    print(scalebar.linkedMap().layers())
    scalebar.setUnitLabel('km')
    scalebar.update()
    layout.addLayoutItem(scalebar)
    scalebar.attemptMove(QgsLayoutPoint(220, 190, QgsUnitTypes.LayoutMillimeters))

    # This exports a Print Layout as an image
    layout = manager.layoutByName(layoutName)

    # This creates a QgsLayoutExporter object
    exporter = QgsLayoutExporter(layout)

    # This exports a pdf of the layout object
    exporter.exportToPdf('C:\\Users\\seteg\\Desktop\\test\\testPDF.pdf', QgsLayoutExporter.PdfExportSettings())

    print("Save Complete")

It is supposed to create a layout and print it onto a PDF file. but now the scale bar looks something like this:

enter image description here

and is all clumped together.

I have edited the code on QGIS Desktop and it works normally, just not on the standalone application, not sure what the issue is

Code on Desktop QGIS:

layersDict = QgsProject.instance().mapLayers()
layers = []
for key, value in layersDict.items():
    layers.append(value)
print(layers)

project = QgsProject.instance()
manager = project.layoutManager()
layoutName = 'Layout1'
layouts_list = manager.printLayouts()
for layout in layouts_list:
    if layout.name() == layoutName:
        manager.removeLayout(layout)
layout = QgsPrintLayout(project)
layout.initializeDefaults()
layout.setName(layoutName)
manager.addLayout(layout)

map = QgsLayoutItemMap(layout)
map.setRect(20, 20, 20, 20)

ms = QgsMapSettings()
print(project.mapLayers())
print(layers)
ms.setLayers(layers)
rect = QgsRectangle(ms.fullExtent())
rect.scale(1.0)
ms.setExtent(rect)
map.setExtent(rect)
map.setBackgroundColor(QColor(255, 255, 255, 0))
layout.addLayoutItem(map)

map.attemptMove(QgsLayoutPoint(5, 20, QgsUnitTypes.LayoutMillimeters))
map.attemptResize(QgsLayoutSize(180, 180, QgsUnitTypes.LayoutMillimeters))

# Scalebar
scalebar = QgsLayoutItemScaleBar(layout)
scalebar.setStyle('Line Ticks Up')
scalebar.setUnits(QgsUnitTypes.DistanceKilometers)
scalebar.setNumberOfSegments(2)
scalebar.setNumberOfSegmentsLeft(0)
scalebar.setUnitsPerSegment(0.5)
scalebar.setLinkedMap(map)
print(scalebar.linkedMap().layers())
scalebar.setUnitLabel('km')
scalebar.update()
layout.addLayoutItem(scalebar)
scalebar.attemptMove(QgsLayoutPoint(220, 190, QgsUnitTypes.LayoutMillimeters))

# This exports a Print Layout as an image
layout = manager.layoutByName(layoutName)

# This creates a QgsLayoutExporter object
exporter = QgsLayoutExporter(layout)

# This exports a pdf of the layout object
exporter.exportToPdf('C:\\Users\\seteg\\Desktop\\test\\testPDF.pdf', QgsLayoutExporter.PdfExportSettings())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.