QCustomPlot Discussion and Comments

Easy way to show/hide QCPItemLine/Rect/etc. ?Return to overview

In my QCP I can add some QCPItemLines and QCPItemRects for analysis of a graph.
I have button for it, the function to show e.g. the QCPItemLines:

...

QCPItemLine *step = new QCPItemLine(plot);
step->setPen(pen);
     
double begin = 25;
double first = plot->yAxis->range().lower;
double end = plot->yAxis->range().upper;   //example values

step->start->setCoords(begin, first);
step->end->setCoords(begin, end);

plot->replot();

...

The items show like I want it, but when I click the button for it again ("uncheck" it), I want to hide these items.
How can I do that?

I found a solution for me, that works like i want it.
On runtime, I create the items after the button click (like the code above).

It can be up to 100 items, thats why I have a vector of type "QCPItemLine" where I add all the items.

When I uncheck the button, I go through all the items like this and delete them:

[header]
...
QVector<QCPItemLine*> steps;
...
[cpp]
...
if(show) //bool show; if true show items, if not hide/remove them
{
    ...
    QCPItemLine *step = new QCPItemLine(plot);
    step->setPen(pen);
      
    double begin = 25; 
    double first = plot->yAxis->range().lower;
    double end = plot->yAxis->range().upper;   //example values
 
    step->start->setCoords(begin, first);
    step->end->setCoords(begin, end);
 
    plot->replot();
}
else
{
    for(int i = 0; i<steps.count(); i++)
    {
         plot->removeItem(steps[i]); //returns true
    }
    plot->replot();
}

item->setVisible(false);