9

Is there any way to order the z-index for some of my QAbstractSeries added to QChart? It seems that QChart does it internally.

milad-gh
  • 113
  • 1
  • 7

3 Answers3

2

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); }
    }
}
Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
0

The problem is that the Q*Series implementations generate independent QGraphicsItems. These are hidden in the private implementation of the series. There is no easy way to access them from the outside.

You could theoretically find them through the scene object (e.g. QGraphicsScene::items()). However I don't see a good way to identify them.

For reference, you can find the class in question here.

ypnos
  • 50,202
  • 14
  • 95
  • 141
0

I know this post is quite old, but I found a solution to this when looking into the QtCharts code, to be specific glxyseriesdata.cpp.

The series is sorted into a map using the series pointer as a key. This map is then iterated for rendering. Setting the z value is thus not really the issue, it is this render ordering.

If you sort your series pointers before passing them to the chart (e.g. by holding them in a vector and using std::sort on it) the render order will be correct.