How to detect window resizing

Dear all,

I have a question about Root GUI programming.
I want to execute a code when the main window is resized. I defined the following slot and connected it to the main frame, but the slot is not executed …
Is there any way to detect window resizing?

void MyClass::HandleResizing(Event_t *e)
  if (e->fType == kConfigureNotify) {
    std::cout << "window resized ?" << std::endl;
    // do something
    // ....

Hi Satoru-san,

This should work… E.g. this code works for me:

[code]#include “TGFrame.h”
#include “TRootEmbeddedCanvas.h”

void HandleResizing(Event_t *e)
if (e->fType == kConfigureNotify) {
std::cout << “window resized !” << std::endl;

  // do something 
  // .... 


void TestResize()
TGMainFrame * w = new TGMainFrame (gClient->GetRoot(), 600, 400);
TRootEmbeddedCanvas *ec = new TRootEmbeddedCanvas(“Ecanvas1”, w, 600, 400);
w->AddFrame(ec, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));

w->Connect(“CloseWindow()”, “TApplication”, gApplication, “Terminate()”);
w->Connect(“ProcessedEvent(Event_t*)”, 0, 0, “HandleResizing(Event_t*)”);

w->SetWindowName(“Example Macro”);
If this macro works for you, and if you still have problem with your code, I would suggest to post your code, or at least a macro reproducing the issue.

Cheers, Bertrand.

Hi Bertrand,

thank you for your reply.
Your code does work but I didn’t notice that, because in a standalone Root program, somehow the standard output is not display in the console. Could you tell me why?

By the way, what I wanted to do is to set the canvas margins when the main window is resized so that the histogram frame on the canvas is always a square. I added the slot “SetMargins()” in the attached script, but the margins are not changed as I expect. I guess it is because the status of the canvas is not updated right after the window is resized.
I’d appeciate any advice…
setmargins.C (1.66 KB)

Hi Satoru,

Sorry, but I cannot tell you why it doesn’t work… do you create a TApplication and call TApplication::Run() inside your main()?

Well, one possible solution could be to use a single shot timer. Just take a look at your modified macro in attachment.

TestMargins.C (1.81 KB)

Hi Bertrand,

Thank you for your reply.

It was due to my wrong code. I have no problem with the standalone program. Sorry to bother you.

It works wonder! Thank you very much.