[QGIS-Developer] Re-sizing legend item
Brian Haynes
bhaynes at herricktechlabs.com
Thu Feb 10 10:07:52 PST 2022
I am attempting to display a legend for a mesh map layer in a custom QGIS application (3.20 Odense, C++). I've previously just added the layer tree view to a dock widget, but it took too much space and the color ramp displayed was still too small. This time I'm using the QgsLayout class to hopefully resize the legend and layout. A code excerpt and the image of my app is below; as you can see you can hardly make out the legend at the bottom. Is there any other way to make the legend bigger? My next idea is to find the bounding box for the legend, take a snapshot, and add as a QPixmap/QLabel widget.
QgsLayout *lay = new QgsLayout(prj->instance()); //prj = QgsProject
lay->setUnits(QgsUnitTypes::LayoutPixels);
QgsLayoutItemLegend *legend = new QgsLayoutItemLegend(lay);
legend->setAutoUpdateModel(false);
QgsLayerTree *tree = legend->model()->rootGroup();
for ( QgsMapLayer *mlay: prj->instance()->mapLayers() )
{
if ( mlay->type() != QgsMapLayerType::MeshLayer )
{
tree->removeLayer(mlay);
}
}
QgsLegendSettings lset = legend->legendSettings();
QgsLegendRenderer rend(legend->model(),lset);
for ( QgsLayerTreeNode *node: tree->children() )
{
rend.setNodeLegendStyle(node,QgsLegendStyle::Hidden);
}
legend->sizeWithUnits().setUnits(QgsUnitTypes::LayoutPixels);
legend->sizeWithUnits().setWidth(400);
legend->sizeWithUnits().setHeight(100);
lay->addLayoutItem(legend);
lay->refresh();
QgsLayoutView *view = new QgsLayoutView();
view->setCurrentLayout(lay);
ui->gridLayout->addWidget(view);
[enter image description here]<https://i.stack.imgur.com/AauId.png>
Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220210/ac14f5ce/attachment.html>
More information about the QGIS-Developer
mailing list