Passing arguments to function writing a GUI

It could, what I can’t do right now is passing these variables to the SetDisplayDetector method, which is in a completely different part of the codebase, so I’d like to take advantage of the slot/signal framework to do this…

If I try to force a value for the boolean (that I might get later from the methods you suggested)

    caloHbutton->Connect("Toggled(Bool_t)", "EDApplication", thisApp, "SetDisplayDetector(=\"Crystal\",=kFALSE)");

still I get the error

Error in <TQObject::CheckConnectArgs>: slot EDApplication::SetDisplayDetector( "Crystal",=kFALSE) does not exist

You could use it in the Slot method (since you have access to the sender). Something like:

   TGCheckButton *but = (TGCheckButton*)gTQSender;
   Bool_t isDown = but->IsDown();
   ...

Great, this works and it’s enough for what I intend to do. Thanks!

Valerio

1 Like

Hi all… i am approaching the GUI methodology in ROOT and i tried to Connect a button to a function with an argument ( a generic object… a Canvas, a TH1 etc.) i tried what is written here bot without success…

I attach here my code adapted from an example found: the button Draw generate 10000 random number and plot in a TH1. the button Max has to find the maximum ( just a simple homework)

exampleGui.C (2.9 KB)

how i should modify the code for make him working? thanks a lot

Giovanni

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")

Thanks a lot!
Does this work in general? Also with more complex functions?

Well, in principle yes, it depends what you want to do…