Hi,
You should make your class inherit from TGMainFrame and remove the RQ_OBJECT(“MainFrame”). It is simpler, and it works (I will investigate why RQ_OBJECT(“MainFrame”) doesn’t work properly).
Here is your code, after a few other minor mods:
[code]#include “TGMenu.h”
#include “TGCanvas.h”
class MainFrame : public TGMainFrame {
private:
TGMenuBar *fMenuBar;
TGPopupMenu *fMenuFile;
TGListTree *fListTree; // left list tree
TGListTreeItem *fBaseLTI; // base (root) list tree item
TGHorizontalFrame *fH0;
TGVerticalFrame *fV0;
TGVerticalFrame *fV1;
TGVerticalFrame *fV2;
TRootEmbeddedCanvas *fRawCanvas;
TRootEmbeddedCanvas *fCalibratedCanvas;
TRootEmbeddedCanvas *fFitCanvas;
public:
MainFrame(const TGWindow *p,UInt_t w,UInt_t h);
virtual ~MainFrame();
void HandleMenu(Int_t);
void FindPeak();
void Fit();
void Calibration();
};
enum EMyMessageTypes {
M_FILE_OPEN,M_FILE_EXIT
};
MainFrame::MainFrame(const TGWindow *p,UInt_t w,UInt_t h) : TGMainFrame(p,w,h)
{
fMenuBar = new TGMenuBar(this, 35, 50, kHorizontalFrame);
fMenuFile = new TGPopupMenu(gClient->GetRoot());
fMenuFile->AddEntry(" &Open…\tCtrl+O", M_FILE_OPEN);
fMenuFile->AddEntry(" &Exit\tAlt+f4", M_FILE_EXIT);
fMenuBar->AddPopup("&File", fMenuFile, new TGLayoutHints(kLHintsTop|kLHintsLeft,0, 4, 0, 0));
AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 2, 2, 5));
fH0 = new TGHorizontalFrame(this,1024,768);
fV0 = new TGVerticalFrame(fH0,200,500);
TGCanvas *canvas = new TGCanvas(fV0, 200, 500);
fListTree = new TGListTree(canvas, kHorizontalFrame);
fListTree->Associate(fV0);
fBaseLTI = fListTree->AddItem(0, “Test”);
fMenuFile->Connect(“Activated(Int_t)”, “MainFrame”,this,“HandleMenu(Int_t)”);
fV0->AddFrame(canvas, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 5, 5,5,5));
fV1 = new TGVerticalFrame(fH0,800,700);
fRawCanvas = new TRootEmbeddedCanvas(“RawCanvas”, fV1, 900, 350);
fCalibratedCanvas = new TRootEmbeddedCanvas(“CalibratedCanvas”, fV1, 900, 350);
fV1->AddFrame(fRawCanvas, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 5, 5,5,5));
fV1->AddFrame(fCalibratedCanvas, new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 5, 5,5,5));
fV2 = new TGVerticalFrame(fH0,350,700);
fFitCanvas = new TRootEmbeddedCanvas(“FitCanvas”, fV2, 350, 350);
fV2->AddFrame(fFitCanvas,new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 5, 5,5,5));
//Peaks
TGTextButton *peaks = new TGTextButton(fV2,"&Find Peaks");
peaks->Connect(“Clicked()”,“MainFrame”,this,“FindPeak()”);
fV2->AddFrame(peaks, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
//Fit
TGTextButton *fit = new TGTextButton(fV2,"&Fit");
fit->Connect(“Clicked()”,“MainFrame”,this,“Fit()”);
fV2->AddFrame(fit, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
//Calibrate
TGTextButton *calibrate = new TGTextButton(fV2,"&Calibration");
calibrate->Connect(“Clicked()”,“MainFrame”,this,“Calibration()”);
fV2->AddFrame(calibrate, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
fH0->AddFrame(fV0);
fH0->AddFrame(fV1);
fH0->AddFrame(fV2);
AddFrame(fH0,new TGLayoutHints(kLHintsLeft | kLHintsExpandY, 5, 5,5,5));
SetWindowName(“Energy Calibrator v1.0”);
// Map all subwindows of main frame
MapSubwindows();
// Initialize the layout algorithm
Resize(GetDefaultSize());
// Map main frame
MapWindow();
}
void MainFrame::HandleMenu(Int_t menu_id)
{
switch (menu_id) {
case M_FILE_OPEN:
{
TGFileInfo file_info_;
TGListTreeItem *item;
TH1 *hpx;
const char *filetypes[] = {"ROOT Files", "*.root", 0, 0};
file_info_.fFileTypes = filetypes;
file_info_.fIniDir = StrDup("C:\\Soleil");
TGFileDialog *dialog = new TGFileDialog(gClient->GetRoot(),gClient->GetRoot(),kFDOpen, &file_info_);
if ( file_info_.fFilename ) {
TFile *file = new TFile(file_info_.fFilename,"UPDATE");
cout << "'" << file_info_.fFilename << "' selected." << endl;
TDirectory *dir = file->GetDirectory("raw");
if (!dir) {
new TGMsgBox(gClient->GetDefaultRoot(), gClient->GetDefaultRoot(),
"Error opening directory",
Form("There is no \"raw\" directory in %s", file_info_.fFilename),
kMBIconExclamation, kMBOk);
dir = gDirectory;
}
TIter next(dir->GetListOfKeys());
TKey *key;
while ((key=(TKey*)next())) { //Loop over keys and add each histo in the ListTree
hpx = (TH1*)dir->Get(key->GetName());
if (!hpx) continue;
const TGPicture *pic = gClient->GetPicture("h1_t.xpm");
item = fListTree->AddItem(fBaseLTI, hpx->GetName(), hpx, pic, pic); //<-- should work now.
}
}
}
break;
}
}
void MainFrame::FindPeak(TH1 *h)
{
}
void MainFrame::Fit()
{
}
void MainFrame::Calibration()
{
}
MainFrame::~MainFrame() {
// Clean up used widgets: frames, buttons, layouthints
Cleanup();
}
void EnergyCalibration()
{
new MainFrame(gClient->GetRoot(),1024,768);
}
[/code]
Cheers, Bertrand.