How to plot a function in a cxx file compiled with g++

I have wrotten a cxx file with ROOT libraries, is a GUI, that could plot a function when I click Draw, but this doesn’t draw nothing. I create an executble, shows the Canvas, but does’t plot. When I compile with root -l this plot the function.

You’re missing at least an event loop if not also a TApplication. In general our recommendation is: just don’t. If you really insist :slight_smile: then this will help:

#include <TApplication.h>

int main(int argc, char **argv)
{
   TApplication theApp("App", &argc, argv);

   // Your code goes here.

   theApp.Run();
   return 0;
}

I wrote that, but the executable doesn’t plot the function. Is possible the loop for doesn’t permit plot the function?

It’s more likely that you have an ownership issue, e.g.

  • there is a file that’s being closed and that’s deleting the function
  • the function is a local variable that goes out of scope
    etc.

Hard to tell without the actual code.

When I compile the executable, terminal shows: Error in TQObject::CheckConnectArgs: slot Dibujar() does not exist
This is the code:

#include "TMath.h"
#include "TGraph.h"
#include "TRandom.h"
#include "TCanvas.h"
#include "Riostream.h"
#include <TGButton.h>
#include <TGFrame.h>
#include <TGClient.h>
#include <TGNumberEntry.h>
#include <TApplication.h>
#include <TAttLine.h>
#include <TGWindow.h>
#include <TGLabel.h>
#include <TGTextEntry.h>
#include <TGButtonGroup.h>
#include <TRootEmbeddedCanvas.h>
#include <RQ_OBJECT.h>
#include <TQObject.h>
#include <TGLayout.h>
#include <TAxis.h>

using namespace std;

TGraph *G;
TCanvas *C;


class Nueva {
  RQ_OBJECT("Nueva")
  private:
  TGNumberEntry *N1, *MAX,*MIN,*AA,*BB,*CC;
  TGLabel *L1,*L2,*L3,*L4,*L5,*L6;
  TGTextButton *B1, *B2;
  TGGroupFrame *Caja1, *Caja2,*Caja3,*Caja4,*Caja5,*Caja6;
  TGMainFrame *Ventana;
  TRootEmbeddedCanvas *Lienzo;
  TGHorizontalFrame *hoframe;
public:
  Nueva(const TGWindow *p, UInt_t w,UInt_t h);
  virtual ~Nueva();
  Double_t GA1(){return N1->GetNumber();}
  Double_t GA2(){return MAX->GetNumber();}
  Double_t GA3(){return MIN->GetNumber();}
  Double_t GA4(){return AA->GetNumber();}
  Double_t GA5(){return BB->GetNumber();}
  Double_t GA6(){return CC->GetNumber();}
  void Dibujar();
};

Nueva::Nueva(const TGWindow *p,UInt_t w,UInt_t h){

//---------------------------
//   MARCO PRINCIPAL
//---------------------------
Ventana=new TGMainFrame(p,w,h);
//-----------------------
Lienzo=new TRootEmbeddedCanvas("Ecanvas",Ventana);
//-------------------------
// Marco secunadario
//-------------------------
hoframe=new TGHorizontalFrame(Ventana);
//--------- Botones
 B1=new TGTextButton(hoframe,"&Ejecutar");
B2=new TGTextButton(hoframe,"&Salir","gApplication->Terminate(0)");
 B1->Connect("Clicked()","Nueva", this,"Dibujar()");
//-------------- Condiciones y parámetros
Caja1=new TGGroupFrame(hoframe,"Tabulaciones");
Caja1->SetLayoutBroken(kTRUE);
Caja2=new TGGroupFrame(hoframe,"L\355mites");
Caja2->SetLayoutBroken(kTRUE);
Caja3=new TGGroupFrame(hoframe,"Par\341metros");
Caja3->SetLayoutBroken(kTRUE);
//------Etiquetas
 L1=new TGLabel(Caja1, "Puntos");
L2=new TGLabel(Caja2, "M\341x");
 L3=new TGLabel(Caja2, "Min");
L4=new TGLabel(Caja3, "a");
 L5=new TGLabel(Caja3, "b");
 L6=new TGLabel(Caja3, "c");
 //---Posición Etiqueta
 L1->MoveResize(35,20,35,10);
 L2->MoveResize(35,20,20,10);
L3->MoveResize(95,20,20,10);
L4->MoveResize(35,20,20,10);
L5->MoveResize(95,20,20,10);
L6->MoveResize(155,20,20,10);
//------------------ entradas de números
N1=new TGNumberEntry(Caja1,0,9,999,
		     TGNumberFormat::kNESReal,
		     TGNumberFormat::kNEAAnyNumber,
		     TGNumberFormat::kNELLimitMinMax,
		     0,20000);

 MAX=new TGNumberEntry(Caja2,0,9,999,
		     TGNumberFormat::kNESReal,
		     TGNumberFormat::kNEAAnyNumber,
		     TGNumberFormat::kNELLimitMinMax,
		     -20000,20000);

MIN=new TGNumberEntry(Caja2,0,9,999,
		     TGNumberFormat::kNESReal,
		     TGNumberFormat::kNEAAnyNumber,
		     TGNumberFormat::kNELLimitMinMax,
		     -20000,20000);
AA=new TGNumberEntry(Caja3,0,9,999,
		     TGNumberFormat::kNESReal,
		     TGNumberFormat::kNEAAnyNumber,
		     TGNumberFormat::kNELLimitMinMax,
		     -20000,20000);

 BB=new TGNumberEntry(Caja3,0,9,999,
		     TGNumberFormat::kNESReal,
		     TGNumberFormat::kNEAAnyNumber,
		     TGNumberFormat::kNELLimitMinMax,
		     -20000,20000);
 CC=new TGNumberEntry(Caja3,0,9,999,
		     TGNumberFormat::kNESReal,
		     TGNumberFormat::kNEAAnyNumber,
		     TGNumberFormat::kNELLimitMinMax,
		     -20000,20000);
 

 //------------------------
// Ubicación
//--------------------------
//____-___________Ventana
 Ventana->SetWMSizeHints(700,600,1600,800,10,10);
 //-----------Disposición de caja
 Caja1->MoveResize(0,0,80,100);
 Caja2->MoveResize(0,0,150,100);
 Caja3->MoveResize(0,0,200,100);
 //------
 N1->MoveResize(20,40,50,20);
 MAX->MoveResize(20,40,40,20);
 MIN->MoveResize(80,40,40,20);
 AA->MoveResize(20,40,50,20);
 BB->MoveResize(80,40,50,20);
 CC->MoveResize(140,40,50,20);
 //--------------Inicializando entrada numérica
 N1->SetNumber(0.0);
 MAX->SetNumber(0.0);
 MIN->SetNumber(0.0);
 AA->SetNumber(0.0);
 BB->SetNumber(0.0);
 CC->SetNumber(0.0);
 //--------
 
 hoframe->AddFrame(B1,new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,5,5,3,4));
hoframe->AddFrame(B2,new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,5,5,3,4));
hoframe->AddFrame(Caja1,new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,5,5,3,4));
 hoframe->AddFrame(Caja2,new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,5,5,3,4));
 hoframe->AddFrame(Caja3,new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,5,5,3,4));

Ventana->AddFrame(Lienzo, new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,10,10,10,1));
Ventana->AddFrame(hoframe, new TGLayoutHints(kLHintsCenterX,2,2,2,2));
//----Nombre al marco principal
Ventana->SetWindowName("Cuadr\341tica");
//-----Subventanas
Ventana->MapSubwindows();
//------Inicializar el algoritmo layout
 Ventana->Resize(Ventana->GetDefaultSize());
 //----Mapear marco principal
 Ventana->MapWindow();
  }

Nueva::~Nueva(){
  Ventana->Cleanup();
  delete Ventana;
}

void Nueva::Dibujar(){
  Double_t a=GA4(),b=GA5(),c=GA6();
  Int_t N=GA1();
  Double_t x,f;
  Double_t min=GA3(),max=GA2();
  
  C =  Lienzo->GetCanvas();
  C->cd();
  C -> SetGrid();
  C->ToggleEditor();
  G= new TGraph();
  //TF1 *f1;
  
  x=min;
  for(Int_t i=0;i<=N;i++){
    //x=(i-N/2)*(max-min)/N;
    f=a*pow(x,2)+b*x+c;
    G->SetPoint(i,x,f);  
    //f1=new TF1("f1","f",0);
    cout<<x<<"\t"<<f<<endl;
    x=x+((max-min)/(double)N);
    }
      
  G->SetTitle("Cuadr\341tica");
 
  G -> GetXaxis() -> SetTitle("x");
  G -> GetYaxis() -> SetTitle("F(x)");
  G -> Draw("AC*");
  //G-> SetLineColor("Red");
  C->SetWindowSize(1000,1000);
  C -> Update();
  C -> Modified();

}

void cuadratica(){
  
  new Nueva(gClient->GetRoot(),300,300);    
}

int main(Int_t argc,Char_t **argv){
  TApplication CuaApp("App",&argc,argv);
  cuadratica();
  CuaApp.Run();
  return 0;
}

Yes that’s likely the reason. @bellenot - can you have a look here?

Hi,

You have to generate a dictionary (using a rootcling or rootcint) for your class in order to be used by the signal/slot mechanism.

Cheers, Bertrand.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.