How to track TCanvas is closed? TCanvas Close() signal

Dear Rooters!
We have a GUI application which uses 2 TCanvas. These canvases are periodically updated during an event loop. The problem is that a user can close the TCanvas during the loop progress. So we need to determine TCanvas state during our update.

First we found out, that if a User closes TCanvas, the object is still exists so we cannot use: if(!fCanvas)…

And we haven’t found how to check TCanvas state.

We have tried to implement some GetMainCanvas() function that should get properly created canvas:



TCanvas* GetMainCanvas(void)
if(fCanvas) delete fCanvas;
fCanvas = new TCanvas(“fCanvas”,“Main View”,400,90,700,500);

	kMainCanvasValid = true;

return fCanvas;


void SlotMainCanvasClosed()
// main canvas closed
kMainCanvasValid = false;


But when a user closes TCanvas (window) , SlotMainCanvasClosed() doesn’t called so kMainCanvasValid is always true. We have thoroughly checked this slot behavior .

Where is the problem or how to solve the problem with TCanvas state tracking?

P.S. We have thought about checking a list of canvases but do not want to iterate through the list each time we need to update TCanvas
P.P.S We use ROOT 5.20 version

1 Like

Test if the canvas is still in the list of canvases


[quote=“brun”]Test if the canvas is still in the lust of canvases

Thank You very much for the reply!

Isn’t there any other way than to iterate through all canvases each time we need to use a canvas?


Here is an example of a possible solution:

[code]void MyMainFrame::CreateCanvas()
if (fCanvas == 0) {
fCanvas = new TCanvas(“c1”,“demo”, 10, 10, 800, 600);
TRootCanvas *rc = (TRootCanvas *)fCanvas->GetCanvasImp();
rc->Connect(“CloseWindow()”, “MyMainFrame”, this, “CanvasClosed()”);

void MyMainFrame::CanvasClosed()

fCanvas = 0;

1 Like

Unfortunatelly, when we set

void MyMainFrame::CanvasClosed() - doesn’t called when user closes window.

Weird… does your MyMainFrame class inherits from TGMainFrame? Do you generate dictionary for it? (you have to if you want to use signal-slot)


[quote=“bellenot”]Weird… does your MyMainFrame class inherits from TGMainFrame? Do you generate dictionary for it? (you have to if you want to use signal-slot)

Yes, I generate a dictionary for it. Also I use signal slots (for example for buttons etc.) in the class and it works in all other cases except closing Canvases. The class does not inherits from TGMainFrame, it inherits from TQObject and uses TGMainFrame as a field.

Then it must work! Could you post your code, to let me see what is going wrong? (otherwise I will not be able to help more)

Cheers, Bertrand.

I have found a simple solution to this same problem, which is to test for the implementation pointer:

TCanvas* c1; .... if (c1->GetCanvasImpl()==NULL) { // window has been closed by user ... }

Ehmm… First of all there is no such member like GetCanvasImpl(). Second of all even if it was if the canvas has been closed the memory is freed (I suppose) and your code would produce a segmentation violation.