I have an application that plots curves in a QwtPlot object. Now, I would like, at will, to show/hide the legend for that QwtPlot object. For this, I am doing something like:

Qt Code:
  1. void GraphPanelPlotWidget::setLegend(const bool &pLegend)
  2. {
  3. // Show/hide our legend
  4.  
  5. if (pLegend != legend())
  6. insertLegend(pLegend?new QwtLegend(this):0);
  7. }
To copy to clipboard, switch view to plain text mode 

As expected, it does create/destroy the legend as requested. However, here are two slightly different scenarios (through my application's GUI):
  1. I show the legend and then create and plot some curves; and
  2. I create and plot some curves and then show the legend.


I would expect both scenarios to yield the exact same result. Yet, although the first scenario gives me what I would expect, i.e.

Screen Shot 2017-11-16 at 18.06.07.jpg

The second scenario doesn't. Instead of the legend itself, I only have some blank space:

Screen Shot 2017-11-16 at 18.04.47.jpg

I had a look at the various Qwt examples, including the 'legends' one, and what I am doing in terms of showing/hiding (or, rather, inserting/removing) the legend is no different. So, why am I getting a different behaviour for my two scenarios, not least since in the 'legends', we may have some curves and no legend, and then decide to show the legend and it will indeed show it right, but... not in my case...!? I am clearly missing something, but I can see what it is.

Thanks in advance to anyone who can help me with this...