QCustomPlot Discussion and Comments

Clean QCustomPlot windowReturn to overview

I need to draw several shapes: QCPItemText, QCPItemLine, QCPAxisRect.
I added a Reset button to clear the screen, but I can’t find which command clears the entire screen, leaving only the coordinate axes.

Thanks!

I found a solution!
Each object must be added as a new Item. And when updating the window, they must be removed.

Example:

QCPItemRect * xRectItem;
QCPItemLine * line;
QCPItemText * textLabel;

// RGB: green color
QColor color_green;
color_green.setRgb(96 , 255 , 96);

// Write text
textLabel = new QCPItemText(ui->widget_Qplot);
ui->widget_Qplot->addItem(textLabel);
textLabel->position->setCoords(70 , 80);
textLabel->setFont(QFont("Sans", 11, QFont::ExtraBold));
textLabel->setText("Text");

// Draw green line
line = new QCPItemLine(ui->widget_Qplot);
ui->widget_Qplot->addItem(line);
line->start->setCoords(100, 10); // location of point 2 in plot coordinate
line->end->setCoords(200, 10); // location of point 1 in plot coordinate
line->setPen(color_green);

// Draw green rectangle 30x40
xRectItem = new QCPItemRect(ui->widget_Qplot);
ui->widget_Qplot->addItem(xRectItem);
xRectItem->setClipToAxisRect(false);
xRectItem->setBrush(color_green);
xRectItem->setPen(QPen(color_green));
xRectItem->topLeft->setType(QCPItemPosition::ptPlotCoords);
xRectItem->bottomRight->setType(QCPItemPosition::ptPlotCoords);
xRectItem->topLeft->setCoords( 10, 10);
xRectItem->bottomRight->setCoords(40 , 50);

// Item counter
int c = ui->widget_Qplot->itemCount();

// Remove all items
for(int i = 0 ; i < c ; i++)
ui->widget_Qplot->removeItem(0);


Did you try
widget_Qplot->clearItems();
widget_Qplot->replot();

Then you don't need all that you wrote.

Right!
Instead of deleting each one, it is fashionable to delete all Items at once.
Thanks!