Hi
I am trying to erase a TPaveText object but I am not doing this operation properly. Because of this, I see that I am creating a bunch of overlap TPaveText objects. I was trying to delete the former TPaveObject before creating a new one but it is not working.
Here I have attached a section of the code. What I am trying to do is read the mouse’s x and y position from the canvas and print these values in a side canvas named “c2”. Every time the mouse moves over the canvas, DynamicExec() function gets called. It seems that as soon as the function finish executing, I loose the pointer to my TPaveText and when I try to call “delete title” (here tile is my TPaveText object) it is deleting the new object instantiated in the function scope of DynamicExec(). Is there a way to retrieve the “old” TPaveText object to remove it from my “c2” canvas?
See my code below for reference to the function scope where the TPaveText is been created and deleted.
Thxs,
Cristian
void xyDisplay()
{
// Create a new canvas.
c1 = new TCanvas(“c1”,“Dynamic Slice Example”,10,10,700,500);
c1->SetFillColor(42);
c1->SetFrameFillColor(33);
Double_t x[100], y[100];
Int_t n = 20;
for (Int_t i=0;i<n;i++) {
x[i] = i0.1;
y[i] = 10sin(x[i]+0.2);
}
gr = new TGraph(n,x,y);
gr->Draw(“AC*”);
c1->AddExec(“dynamic”,“DynamicExec()”);
}
void DynamicExec()
{
TPaveText *title;
char coord[80];
TVirtualPad *padsav = gPad;
TCanvas c2 = TCanvas)gROOT->GetListOfCanvases()->FindObject(“c2”);
if(c2){
delete title;//c2->GetPrimitive(“xycoordenates”);
}
else{
c2 = new TCanvas(“c2”,“xycoordenates”,710,30,300,200);
}
c2->cd();
sprintf(coord,"[%4.3f,%4.3f]",5.0,6.0);
title = new TPaveText(0.1,0.6,0.9,0.98);
title->AddText("[X,Y] coord");
title->AddText(coord);
title->Draw();
c2->Update();
padsav->cd();
}