<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<div class="s-prose js-post-body">
<p>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.</p>
<pre class="default s-code-block"><code class="hljs language-php">QgsLayout *lay = <span class="hljs-keyword">new</span> QgsLayout(prj->instance()); <span class="hljs-comment">//prj = QgsProject</span>
lay->setUnits(QgsUnitTypes::LayoutPixels);
QgsLayoutItemLegend *legend = <span class="hljs-keyword">new</span> QgsLayoutItemLegend(lay);
legend->setAutoUpdateModel(<span class="hljs-literal">false</span>);
QgsLayerTree *tree = legend->model()->rootGroup();

<span class="hljs-keyword">for</span> ( QgsMapLayer *mlay: prj->instance()->mapLayers() )
{
    <span class="hljs-keyword">if</span> ( mlay->type() != QgsMapLayerType::MeshLayer )
    {
        tree->removeLayer(mlay);
    }
}

QgsLegendSettings lset = legend->legendSettings();
QgsLegendRenderer rend(legend->model(),lset);

<span class="hljs-keyword">for</span> ( QgsLayerTreeNode *node: tree->children() )
{
    rend.setNodeLegendStyle(node,QgsLegendStyle::Hidden);
}

legend->sizeWithUnits().setUnits(QgsUnitTypes::LayoutPixels);
legend->sizeWithUnits().setWidth(<span class="hljs-number">400</span>);
legend->sizeWithUnits().setHeight(<span class="hljs-number">100</span>);
lay->addLayoutItem(legend);
lay->refresh();
QgsLayoutView *view = <span class="hljs-keyword">new</span> QgsLayoutView();
view->setCurrentLayout(lay);
ui->gridLayout->addWidget(view);
</code></pre>
<p><a href="https://i.stack.imgur.com/AauId.png" rel="nofollow noreferrer"><img alt="enter image description here" src="https://i.stack.imgur.com/AauId.png"></a></p>
</div>
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Brian</div>
</body>
</html>