My simple GUI example is crashing

I’m trying to do something fairly simple. Add labels to a frame and add that frame to
a canvas. Here’s my code…

//MyMainFrame.h
#include <TRootEmbeddedCanvas.h>
#include <TGFrame.h>
#include <TApplication.h>
#include <TGLabel.h>

class MyMainFrame : public TGMainFrame{

private:
TRootEmbeddedCanvas fECanvas;
TGHorizontalFrame
fHFrame;
TGLabel* fNumberEntryLabel;

public:
MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MyMainFrame();

ClassDef(MyMainFrame,1);
};

//My MainFrame.cxx
#include “rootanalysis/MyMainFrame.h”

ClassImp(MyMainFrame);

MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h):TGMainFrame(p,w,h){
fECanvas = new TRootEmbeddedCanvas(“ECanvas”,this,200,200);
AddFrame(fECanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,10,10,10,1));

fHFrame = new TGHorizontalFrame(fECanvas,200,40);

fNumberEntryLabel = new TGLabel(fHFrame,“No. Entries”);
fHFrame->AddFrame(fNumberEntryLabel, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY ,5,5,3,4));

AddFrame(fHFrame,new TGLayoutHints(kLHintsCenterX,2,2,2,2));

//Sets window name and shows the main frame
SetWindowName(“Simple Example”);
MapSubwindows();
Resize(GetDefaultSize());
MapWindow();
}

MyMainFrame::~MyMainFrame(){
delete fECanvas;
delete fNumberEntryLabel;
delete fHFrame;
}

This compiles just fine, but when I run it seg faults at AddFrame.

(gdb) where
#0 0x0318ecef in raise () from /lib/tls/libc.so.6
#1 0x031904f5 in abort () from /lib/tls/libc.so.6
#2 0x010a4cee in TUnixSystem::Abort () from /disk01/software/root/pro/lib/libCore.so
#3 0x010a352c in TUnixSystem::DispatchSignals () from /disk01/software/root/pro/lib/libCore.so
#4 0x010a21db in SigHandler () from /disk01/software/root/pro/lib/libCore.so
#5 0x010a7097 in sighandler () from /disk01/software/root/pro/lib/libCore.so
#6
#7 0x00000000 in ?? ()
#8 0x01b667e3 in TGCompositeFrame::AddFrame () from /disk01/software/root/pro/lib/libGui.so
#9 0x00c57789 in MyMainFrame (this=0xa84df80, p=0xa4b6998, w=200, h=220) at private/rootanalysis/MyMainFrame.cxx:14

I imagine I’m doing something that’s very obviously wrong, but I’ve been staring at this
for awhile and am running out of ideas. Any help or suggestions would be greatlly
appreciated.
Thanks,
Alex.

Hi Alex,

A canvas is the area in which you can create and/or modify images. You cannot use it as a container for GUI widgets. A TGCompositeFrame should be use instead. Please read the chapter ftp://root.cern.ch/root/doc/chapter23.pdf and see the example on the page 393.

Best regards, Ilka

Hi,

is wrong.

Did you try to do the same with
gui builder?

Regards. Valeriy

Thanks! Changing that one line fixed the problem.

//fHFrame = new TGHorizontalFrame(fECanvas,200,40);
fHFrame = new TGHorizontalFrame(this,200,40);

I have another stupid question though. What’s gui builder?
Alex.

Hi Alex,
with ROOT 4.02
you can try
root[]new TGuiBuilder :slight_smile:

Regards. Valeriy

So does this not come with 4.00.08? It doesn’t look like
it does. I think I’m going to upgrade soon, but I’m not sure
how well our code works with the new ROOT version.
I forget all of the issues involved and what has and hasn’t
been resolved, but I looked at TRootGuiBuilder and it looks
pretty cool. :slight_smile:
Alex.