QCustomPlot Discussion and Comments

Block until the chart is completely replot() before delete the sourceReturn to overview

Hello,
i plot a very large number of QCPItemLine (arrows) on a chart. the data for arrow->start and arrow->end are in a std::vector. This vector is iterated in a loop and the item objects are created there. With replot() the created items should be displayed.
After that I want to free the memory and empty the vector with .clear() and .shrink_to_fit().

// first the loop that create all the QCPItemLine Objects. after that:
ui->customchart->replot();
vector.clear();
vector.shrink_to_fit();

The program crashes this way!
I think the reason is probably because replot() doesn't block until the completion of drawing the items (could work asyncron). The vector will be deleted before replot() is complete and there is no more data at the location for drawing the items.

How can you make replot() block until the objects are completely drawn on the chart?

I tried the variants like "QCustomPlot::rpImmediateRefresh" etc but even then it crashes.
if i use std::this_thread::sleep_for(std::chrono::milliseconds(x)) to give replot more time, then it also blocks drawing in the chart.

Does anyone have a solution how I can delete the vector after replot() but not before the replot() has finished drawing in GUI? Is there a possibility to block until the chart is completely drawn (before deleting the vector)?

is the vector that you are using owned by the plot? when you pass data into qcustomplot normally, it copies the data into its own structure. if you delete the data that it owns at any time, you are going to crash, even if it is after the replot is completely finished.

the vector contains a struct with the data:

struct struct_v
{
   double start_x;
   double start_y;
   double end_x;
   double end_y;
};

std::vector<struct_v> vector;

The vector using by the loop to create the QCPItemLine Objects:

for(long i = 0; i < vector.size(); ++i)
{
    QCPItemLine *arrow = new QCPItemLine(ui->CP);
    arrow->start->setCoords(vector[i].start_x, vector[i].start_y);
    arrow->end->setCoords(vector[i].end_x, vector[i].end_y);
    arrow->setHead(QCPLineEnding::esLineArrow);
    arrow->setPen(QPen(Qt::darkGreen,2)); 
}

ui->CP->replot();

vector.clear();
vector.shrink_to_fit();

This way, it crashes. When i do not use clear() and shrink_to_fit() it works. But i want to free the vector memory and empty the vector after the plot.

that is really odd. qcustomplot wouldn't own those values, so it makes absolutely no sense that it would have anything to do with the replot. Those functions are just copying the value to their own variables.

Are you sure that you arent crashing for a reason unrelated to the qcustomplot? your vector should be unrelated if that's how you are using it. do you have a slot connected to the qcustomplot that uses the vector on your side after you deleted it?