How to change the z-index of QLineSeries?

Zlevel is set by QtCharts::ChartItem that are stored in QtCharts::ChartPresenter are hidden in the private part of QtChart. We can get to it by applying the findChild() method.

ChartPresenter has a method to get its items, but you need to know how you will differentiate them (as a name is assigned to a series.) I have used the opacity property for this purpose. Keep in mind that when assigning a new z level, scenes can be lower (for example Legend.)

void AppDispatcher::setZLevel(QtCharts::QXYSeries *series)
{
    QtCharts::ChartPresenter* present = series->chart()->findChild<QtCharts::ChartPresenter*>();
    Q_ASSERT(present);

    QList<QtCharts::ChartItem *> items = present->chartItems();

    for(QtCharts::ChartItem * item : items){
        if(item->opacity() == 0.99) { item->setZValue(QtCharts::ChartPresenter::ZValues::SeriesZValue+3); item->setOpacity(1); }
        if(item->opacity() == 0.98) { item->setZValue(QtCharts::ChartPresenter::ZValues::SeriesZValue+2); item->setOpacity(1); }
        if(item->opacity() == 0.97) { item->setZValue(QtCharts::ChartPresenter::ZValues::SeriesZValue+1); item->setOpacity(1); }
    }
}

Tags:

Qt

Qtcharts