I tried to make the "add html" item in the layout display a paragraph of text.This code works well in Python console:
project = QgsProject.instance()
layout_manager = project.layoutManager()
template_layout = layout_manager.layoutByName('test')
layout = template_layout.clone()
item = layout.itemById('html')
if isinstance(item , QgsLayoutFrame):
multi_frame = item.multiFrame()
if isinstance(multi_frame, QgsLayoutItemHtml):
html = multi_frame
item.setFrameEnabled(False)
html.setContentMode(QgsLayoutItemHtml.ManualHtml)
html.setHtml('this is a test!')
html.loadHtml()
settings = QgsLayoutExporter.ImageExportSettings()
settings.dpi = 300
output_path = r"D:\a.jpg"
exporter = QgsLayoutExporter(layout)
exporter.exportToImage(output_path, settings)
layout_manager.removeLayout(layout)
But the same code cannot work properly in the Processing ToolBox:
def processAlgorithm(self,parameters: dict[str, Any],context: QgsProcessingContext,feedback: QgsProcessingFeedback,) -> dict[str, Any]:
...
project = context.project()
layout_manager = project.layoutManager()
template_layout = layout_manager.layoutByName('test')
layout = template_layout.clone()
item = layout.itemById('html')
if isinstance(item , QgsLayoutFrame):
multi_frame = item.multiFrame()
if isinstance(multi_frame, QgsLayoutItemHtml):
html = multi_frame
item.setFrameEnabled(False)
html.setContentMode(QgsLayoutItemHtml.ManualHtml)
html.setHtml('this is a test!')
html.loadHtml()
settings = QgsLayoutExporter.ImageExportSettings()
settings.dpi = 300
output_path = r"D:\b.jpg"
exporter = QgsLayoutExporter(layout)
exporter.exportToImage(output_path, settings)
layout_manager.removeLayout(layout)
...
What happened and how should I modify it?
-
gis.stackexchange.com/questions/493906/…Ben W– Ben W2025年06月26日 13:22:49 +00:00Commented Jun 26 at 13:22
-
1thank you!thank you very much!user316887– user3168872025年06月26日 13:37:02 +00:00Commented Jun 26 at 13:37
-
1Please provide the exact QGIS version used and explain what do you mean exactly with <<cannot work properly>>.Andrea Giudiceandrea– Andrea Giudiceandrea2025年06月26日 14:11:55 +00:00Commented Jun 26 at 14:11
-
1html item display nothing when the code run in the Processing ToolBox. thanks Ben W,i get "processing algorithms are run in a background thread by default and any non-thread-safe API calls made in the processAlgorithm() method will result in crashes & unexpected behavior".user316887– user3168872025年06月28日 11:51:37 +00:00Commented Jun 28 at 11:51
default