Uwe Rathmann - 2022-12-12

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().