QCustomPlot Discussion and Comments

How do I remove a QCPCurve after the user does some action?Return to overview

I have written a program that - after the user enters some datapoints and presses a button - plots a curve on a widget.
This all works very fine!

However, I want to give the user to possibility to change the plot without closing my program and restarting it.
That is, when the user enters some new datapoints in the textEdits and presses the button again the plot updates and
erases the old plot.

In my current program, the plot updates after entering new values but the old plot is still visible.
Therefore I am looking for a way to remove the old QCPCurve when the new is created.
Current code looks something like this:

void MainWindow::on_btn_edrp_clicked()
{

QCPCurve *box = new QCPCurve(ui->plot->xAxis, ui->plot->yAxis);
    QVector<QCPCurveData> boxvec_x(2);

    //Read the user input - Convert QString to std::string to double
    QString qstringpilot = ui->textEdit_pilot->toPlainText();
    std::string stringpilot = qstringpilot.toStdString();
    pilot = std::stod(stringpilot);

    QString qstringcopilot = ui->textEdit_copilot->toPlainText();
    std::string stringcopilot = qstringcopilot.toStdString();
    copilot = std::stod(stringcopilot);

    boxvec_x[0] = QCPCurveData(0,20,pilot);
    boxvec_x[1] = QCPCurveData(1,30,copilot);
  

    box->data()->set(boxvec_x, true);
    ui->plot->replot();

}

Can someone help me find a way to accomplish this?

I find!

ui->plot->removePlottable(box);