Re: [ROOT] TGButton SetCommand query

From: Paul Dixon (paul.dixon@surfability.com)
Date: Tue Apr 23 2002 - 11:44:48 MEST


Hi,

(I've appended  some source code and a Makefile below, hope no one minds - just
type gmake Makefile to compile under linux).

I'm still having trouble with creating a button that can access a method
via SetCommand...

I've followed the instructions and created the dictionary files in order
to get CINT to "know" about my class, but when I run the program, I get
the following:

[~/root] $ ./canvasgui
Hello World

<I click on the PlotMe button>

Error: Symbol gPlotMe is not defined in current scope  FILE:/tmp/fileSwUcgo_cint LINE:1
Error: Failed to evaluate gPlotMe->Sinx()Possible candidates are...
filename       line:size busy function type and name  
*** Interpreter error recovered ***


I've exhausted my current knowledge... can anyone point me in the right
direction? 

Cheers,
                                        Paul

Files below are: PlotMe.h
                 PlotMe.cxx
                 PlotMeLinkDef.h
                 canvasgui.cxx
                 Makefile



//
//                PlotMe.h
//


#ifndef __PLOTME_H
#define __PLOTME_H

#include <iostream.h>
#include <TROOT.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRootEmbeddedCanvas.h>


class PlotMe {

      public:
         PlotMe();
         ~PlotMe();
         void Sinx(void);

         ClassDef (PlotMe, 1)

      private:
         //none
};

R__EXTERN PlotMe *gPlotMe;

#endif

/////////////////// End of PlotMe.h ////////////////////





//
//                         PlotMe.cxx
//

#ifndef __PLOTME_H
#include "PlotMe.h"
ClassImp (PlotMe);
#endif


// constructor
PlotMe::PlotMe()
{
}

// destructor
PlotMe::~PlotMe()
{
}

// methods
void PlotMe::Sinx(void)
{
//   TRootEmbeddedCanvas *c;
//   TCanvas *c1 = c->GetCanvas();
   cout<<"Hello World"<<endl;
   TF1 f1("func1", "sin(x)/x", 0, 10);
   f1.Draw(); 
}


/////////////////////// End of PlotMe.cxx ////////////////////






//
//                PlotMeLinkDef.h
//


#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ class PlotMe;

#endif

////////////////// End of PlotMeLinkDef.h ///////////////




//
//              canvasgui.cxx
//


#include <TApplication.h>
#include <TGButton.h>
#include <TRootEmbeddedCanvas.h>
#include <TFile.h>
#include <TF1.h>
#include <TCanvas.h>

#include "PlotMe.h"

int main()
{
   // Create application environment
   TApplication app("test", 0, 0);

   // Create main frame
   TGMainFrame mf(gClient->GetRoot(), 100, 100);

   // Create embedded canvas and add to main frame, centered in x and y and 
   // with 30 pixel margins all around
   TRootEmbeddedCanvas *c = new TRootEmbeddedCanvas("Canvas", &mf, 600, 400);

   mf.AddFrame(c, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY,
                                    30, 30, 30, 30));

   // Create button and add to main frame, centered in x and with 30
   // pixel margin at bottom, when button is clicked terminate program
   TGButton *b1 = new TGTextButton(&mf, "Plot");
   TGButton *b2 = new TGTextButton(&mf, "Quit");
   b1->SetCommand("gPlotMe->Sinx()");
   b2->SetCommand("gApplication->Terminate()");

   TCanvas *c1 = c->GetCanvas();
   PlotMe *pt = new PlotMe();
   pt->Sinx();
   c1->Update();

   mf.AddFrame(b1,new TGLayoutHints(kLHintsCenterX, 0, 0, 0, 30));
   mf.AddFrame(b2,new TGLayoutHints(kLHintsCenterX, 0, 0, 0, 40));

   // Set main frame name, map sub windows (button), initialize layout
   // algorithm via Resize() and map main frame 
   mf.SetWindowName("Test");
   mf.MapSubwindows();
   mf.Resize(mf.GetDefaultSize());
   mf.MapWindow();

   // enter application event loop
   app.Run();

   return 0;
}



#
#         Makefile for canvasgui
#

ROOTCFLAGS   := $(shell root-config --cflags)
ROOTLIBS     := $(shell root-config --libs)
ROOTGLIBS    := $(shell root-config --glibs)
CC = g++

all: PlotMeDict.cxx PlotMeDict.o PlotMe.o canvasgui

PlotMeDict.cxx: PlotMe.h PlotMeLinkDef.h
        @echo "Generationg Dictionary $@..."
        @rootcint -f $@ -c $^

PlotMeDict.o: PlotMeDict.cxx
        @echo "Compiling $< ..."
        @$(CC) $(ROOTCFLAGS) -c $^


PlotMe.o:       PlotMe.cxx PlotMe.h
        @echo "Compiling $< ..."
        @$(CC) $(ROOTCFLAGS) -c PlotMe.cxx

canvasgui: canvasgui.cxx PlotMe.o PlotMeDict.o
        @echo "Compiling $< ..."
        @$(CC) $(ROOTCFLAGS) $(ROOTLIBS) $(ROOTGLIBS) $^ -o $@

clean:
        @rm -f PlotMe.o libPlotMe.so PlotMeDict.cxx \
                PlotMeDict.cxx canvasgui


############### End of Makefile ################



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:51 MET