What you’re trying to do cannot work: The arguments must be the same in the signal than in the slot. But you can access the hist
class member directly from the Massimo()
method:
#include <TGClient.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TRootEmbeddedCanvas.h>
class MyMainFrame : public TGMainFrame {
private:
TRootEmbeddedCanvas *fEcanvas;
//TF1 *f1;
public:
int i;
MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h);
virtual ~MyMainFrame();
TF1 *f1;
TH1F *hist;
float randon;
void DoDraw();
void Massimo();
};
MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h) : TGMainFrame(p,w,h), hist(0) {
// Create canvas widget
fEcanvas = new TRootEmbeddedCanvas("Ecanvas",this,500,200);
AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX |
kLHintsExpandY, 10,10,10,1));
// Create a horizontal frame widget with buttons
TGHorizontalFrame *hframe = new TGHorizontalFrame(this,200,40);
TGTextButton *draw = new TGTextButton(hframe,"&Draw");
draw->Connect("Clicked()","MyMainFrame",this,"DoDraw()");
hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,
5,5,3,4));
TGTextButton *Max = new TGTextButton(hframe,"&Max");
Max->Connect("Clicked()","MyMainFrame",this,"Massimo()");
hframe->AddFrame(Max, new TGLayoutHints(kLHintsCenterX,
5,5,3,4));
TGTextButton *exit = new TGTextButton(hframe,"&Exit",
"gApplication->Terminate(0)");
hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX,
5,5,3,4));
AddFrame(hframe, new TGLayoutHints(kLHintsCenterX,
2,2,2,2));
// Set a name to the main frame
SetWindowName("Simple Example");
// Map all subwindows of main frame
MapSubwindows();
// Initialize the layout algorithm
Resize(GetDefaultSize());
// Map main frame
MapWindow();
}
void MyMainFrame::DoDraw() {
//Draws function graphics in randomly chosen interval
//TF1 *f1 = new TF1("f1","3*x*x-5*x",0,gRandom->Rndm()*10);
// f1->SetLineWidth(3);
// f1->Draw();
hist = new TH1F("","",100,-10,10);
for(i=0;i<10000;i++){
randon = gRandom->Gaus(0,2);
hist->Fill(randon);
hist->Draw();
}
TCanvas *fCanvas = fEcanvas->GetCanvas();
fCanvas->cd();
fCanvas->Update();
}
void MyMainFrame::Massimo() {
// Draws function graphics in randomly chosen interval
cout<<"WOW"<<endl;
if (hist) {
float max = hist->GetMaximum();
cout << max << endl;
}
}
MyMainFrame::~MyMainFrame() {
// Clean up used widgets: frames, buttons, layout hints
Cleanup();
}
void example() {
new MyMainFrame(gClient->GetRoot(),500,200);
}
And BTW, I simplified a bit your code, making your class inheriting from a TGMainFrame, and removing the RQ_OBJECT("MyMainFrame")