0

I followed qt documentation https://doc-snapshots.qt.io/qt5-5.11/qtcharts-legend-example.html to detach the legend from the chart and set the detached legend dimensions

legend->detachFromChart();
m_chart->legend()->setBackgroundVisible(true);
m_chart->legend()->setBrush(QBrush(QColor(128, 128, 128, 128)));
m_chart->legend()->setPen(QPen(QColor(192, 192, 192, 192)));
m_chart->legend()->setGeometry(QRectF(80, 50, 100, 180));

m_chart->legend()->setGeometry(QRectF(80, 50, 100, 180)) does not work. the first two arguments works. The position of the legend is (80, 50). However, the last two arguments does not work. The width of the legend is not 100, and the height of the legend is not 180.

lll
  • 302
  • 3
  • 13

2 Answers2

1

You should call update() function of QLegend after setting geometry:

m_chart->legend()->update();
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • i added m_chart->legend()->update(), but width and height of the legend are not changed – lll Apr 18 '19 at 15:09
0

Update legend after setting the geometry of the legend. m_chart->legend()->update().

Important note: set legend's geometry after adding the lineseries . this is worked for me . When we set properties of legend & chart before adding the lineseries, it won't work. Even in some case, it leads to application crash.