QwtLegend behaves weird
Brought to you by:
rathmann
Consider the following code:
auto curve1 = createCurve("Curve 1"); // creates an example curve
curve1->setZ(1000); // the setZ line
curve1->attach(myPlot);
auto curve2 = createCurve("Curve 2");
curve2->attach(myPlot);
auto curve3 = createCurve("Curve 3");
curve3->attach(myPlot);
auto legend = new QwtLegend();
myPlot->insertLegend(legend, QwtPlot::BottomLegend);
This will cause the legend to display the following (wrong) order: "Curve 2" "Curve 3" "Curve 1"
If I remove the setZ line then the (correct) order is "Curve 1" Curve 2" "Curve 3"
The correct order is also displayed when I leave the setZ line as it is and create and insert the legend before I attach the curves.
Anonymous
The problem here is, that the the items are stored in the plot according to the z-order first and the order how they have been inserted is lost when having different z-values.
That explains ( not excuses ) why the order on the legend is different when attaching an item to a plot with a legend compared to adding the legend to a plot with items.
For the second situation I do not see an obvious way to display the legend items ordered according how they have been inserted - simply because this order is stored nowhere.
In case items on the legend should be sorted in a specific way it can be done by overloading QwtLegend::updateLegend. If additional information about the item is needed to do the sorted it can be added by overloading QwtPlotItem::updateLegendData().