N
N
NosferatuZodd2021-11-27 13:37:53
Qt
NosferatuZodd, 2021-11-27 13:37:53

It is necessary to add a check whether one class object already exists and in this case remove it from memory and create a new one, what should be done?

The bottom line is that when I click on the button, I call the "drawChart" slot that draws the chart, for this, every time I click the button, I create a new object of the "Chart" class. So the problem is that I can create such objects indefinitely, but I need to delete the old chart and draw a new one when the user clicks on the second / third / etc. button. once.

void MainWindow::drawChart(){
    chart = new Chart(line_x0->text().toFloat(), line_yx0->text().toFloat(), this);
    layoutChart->addWidget(chart);
    layoutMain->addLayout(layoutChart);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel K, 2021-11-29
@PavelK

Greetings.
Why not then declare the pointer to the chart as a member of the class, initialize it in the constructor (well, or at the first use), and when you need to draw a new one, just clear the old chart with its own methods, and not recreate it every time anew?

D
Dimchik, 2021-12-04
@Dimpik

This is done through something like child () or get_child (), look on the internet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question