TTimer not firing in standalone ROOT GUI application

Hi,

I’m trying to make my first ROOT GUI application. Right now, it successfully opens a TGMainFrame-derived window, which can be closed to end the program. The next thing I’m trying to add is a TTimer to periodically update the (currently empty) frame, but I’m also new to TTimers and I can’t seem to get any signals out of it.

As I understand it, my code should open a GUI, call HandleTimer(Timer*) (inherited from TObject) every 100ms, and then exit when the GUI is closed, giving a notification via console output at each of those events. However, when I run it, the constructor and destructor work as intended (including console output) but I get no notifications from HandleTimer.

#include <iostream>

#include <TApplication.h>
#include <TCanvas.h>
#include <TTimer.h>

#include <TGClient.h>
#include <TGFrame.h>
#include <TRootEmbeddedCanvas.h>

using namespace std;

class DAQMainFrame : public TGMainFrame{
private:
	TApplication* daqApp;
	TRootEmbeddedCanvas* canvas;
	TTimer* timer;
public:
	DAQMainFrame(TApplication*);
	virtual ~DAQMainFrame();
	Bool_t HandleTimer(TTimer*);
};
DAQMainFrame::DAQMainFrame(TApplication* app) : TGMainFrame(gClient->GetRoot()){
	//I get this notification, and the window opens
	cout<<"DAQMainFrame constructor called\n";
	daqApp = app;
	
	canvas = new TRootEmbeddedCanvas("Ecanvas",this,800,600);
	AddFrame(canvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 10,10,10,1));

	SetWindowName("TPTPC DAQ");
	MapSubwindows();
	Resize(GetDefaultSize());
	MapWindow();	
	daqApp->Run();
	
	timer = new TTimer(this, 100);//create a TTimer to call the frame's HandleTimer(TTimer*)
	timer->TurnOn();
}
DAQMainFrame::~DAQMainFrame(){
	//I get this notification when the window closes
	cout<<"DAQMainFrame destructor called\n";
	Cleanup();
	daqApp->Terminate();
	timer->TurnOff();
}
Bool_t DAQMainFrame::HandleTimer(TTimer*){
	//I never actually get this notification
	cout<<"DAQMainFrame::HandleTimer called\n";
	return kTRUE; 
}

int main(){
	TApplication* daqApp = new TApplication("daqApp",0,0);
	new DAQMainFrame(daqApp);
}

Can anyone see what I’m doing wrong? I’m also a bit rusty at C++ inheritance, so the problem might also be there.

Thanks,
Callan


ROOT Version: 5.34/36
Platform: 3.2.0-48-generic #74-Ubuntu SMP Thu Jun 6 19:43:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Compiler: g++


Do:

int main()
{
   TApplication* daqApp = new TApplication("daqApp",0,0);
   new DAQMainFrame(daqApp);
   daqApp->Run();
   return 0;
}

Then:

DAQMainFrame::~DAQMainFrame() {
	//I get this notification when the window closes
	cout<<"DAQMainFrame destructor called\n";
	Cleanup();
	timer->TurnOff();
	daqApp->Terminate();
}

i.e. call timer->TurnOff(); before daqApp->Terminate();, and finally remove daqApp->Run(); from the DAQMainFrame constructor

Cheers, Bertrand.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.