Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the z-index of QLineSeries?

Tags:

qt

qtcharts

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

like image 784
milad-gh Avatar asked Feb 05 '17 11:02

milad-gh


1 Answers

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); }
    }
}
like image 182
Петр Кусоцкий Avatar answered Nov 15 '22 10:11

Петр Кусоцкий