A layer that may contain objects, to control the rendering order.
The Layering system of QCustomPlot is the mechanism to control the rendering order of the elements inside the plot, e.g. that the grid is drawn behind plottables etc.
It is based on the two classes QCPLayer and QCPLayerable. A QCustomPlot contains an ordered list of one or more instances of QCPLayer (see QCustomPlot::addLayer, QCustomPlot::layer, QCustomPlot::moveLayer, etc.). The layers are drawn in the order they are in the list.
A QCPLayer itself contains an ordered list of QCPLayerable instances. QCPLayerable is an abstract base class from which almost all visible objects derive, like axes, grids, graphs, items, etc.
By default, QCustomPlot has three layers: "grid", "main" and "axes" (in that order). Initially the QCPGrid instances are on the "grid" layer, so the grid will be drawn beneath the objects on the other two layers. The top layer is "axes" and contains all four axes, so they will be drawn on top. Between these two layers, there is the "main" layer. It is initially empty and set as the current layer (see QCustomPlot::setCurrentLayer). This means, all new plottables, items etc. are created on this layer by default, and are thus drawn above the grid but below the axes.
Controlling the ordering of objects is easy: Create a new layer in the position you want it to be, e.g. above "main", with QCustomPlot::addLayer. Then set the current layer with QCustomPlot::setCurrentLayer to that new layer and finally create the objects normally. They will be placed on the new layer automatically, due to the current layer setting. Alternatively you could have also ignored the current layer setting and just moved the objects with QCPLayerable::setLayer to the desired layer after creating them.
It is also possible to move whole layers. For example, If you want the grid to be shown in front of all plottables/items on the "main" layer, just move it above "main" with QCustomPlot::moveLayer. This way the ordering might now be "main", "grid", "axes", so while the grid will still be beneath the axes, it will now be drawn above plottables/items on "main", as intended.
The rendering order within one layer is simply by order of creation. The item created last (or added last to the layer), is drawn on top of all other objects on that layer.
When a layer is deleted, the objects on it are not deleted with it, but fall on the layer below the deleted layer, see QCustomPlot::removeLayer.