Root cern GUI: emit vector

Hello!

I write GUI and try to send signal with vector.
There is small example (https://root.cern.ch/root/htmldoc/guides/users-guide/WritingGUI.html#a-simple-example, 27.7 Event Processing: Signals and Slots) how to send Int_t type.

//root cern threads
#include "TThread.h"
#include <Riostream.h>

//root cern general
#include <TApplication.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>

//root cern GUI
#include <TGClient.h>
#include <TGButton.h>
#include <TRootEmbeddedCanvas.h>
#include "TGFrame.h"
#include "TGTab.h"
#include <TQObject.h>
#include <RQ_OBJECT.h>

using namespace std;

class MyClass
{
   RQ_OBJECT("MyClass")
private:
   Int_t fValue;
public:
   MyClass() { fValue=0; }
   Int_t GetValue() const { return fValue; }
   void  SetValue(Int_t); // *SIGNAL*
};

void MyClass::SetValue(Int_t v)
{
   if (v != fValue)
   {
      fValue = v;
      Emit("SetValue(Int_t)",v);
   }
}

void siglan_slot_int()
{
    MyClass *objA = new MyClass();
    MyClass *objB = new MyClass();
    objA->Connect("SetValue(Int_t)","MyClass",objB,"SetValue(Int_t)");

    //objB->SetValue(11);
    objA->SetValue(79);
    cout << "objA->GetValue()" << objA->GetValue() << endl;
    cout << "objB->GetValue()" << objB->GetValue() << endl;
}

This code works for Int_t .

I need the same for vector, for example.

//root cern threads
#include "TThread.h"
#include <Riostream.h>

//root cern general
#include <TApplication.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>

//root cern GUI
#include <TGClient.h>
#include <TGButton.h>
#include <TRootEmbeddedCanvas.h>
#include "TGFrame.h"
#include "TGTab.h"
#include <TQObject.h>
#include <RQ_OBJECT.h>


//using namespace std;


class MyClass
{
   RQ_OBJECT("MyClass")
private:
   std::vector<Int_t> fValue_vec;
public:
   MyClass() { fValue_vec.resize(5); }
   Int_t GetValue() const { return fValue_vec; }
   void  SetValue(std::vector<Int_t>); // *SIGNAL*
};

void MyClass::SetValue(std::vector<Int_t> v)
{
    for (int i = 0; i < fValue_vec.size(); ++i)
    {
        fValue_vec[i] = v[i];
    }

    Emit("SetValue(std::vector<Int_t>)",v);
}

void signal_slot_vector()
{
    MyClass *objA = new MyClass();
    MyClass *objB = new MyClass();
    objA->Connect("SetValue(std::vector<Int_t>)","MyClass",objB,"SetValue(std::vector<Int_t>)");

    std::vector<Int_t> vec_a;
    vec_a.push_back(1);
    vec_a.push_back(2);
    vec_a.push_back(3);
    vec_a.push_back(4);
    vec_a.push_back(5);

    objA->SetValue(vec_a);

}

Output of this code:

Error: Function vector<Int_t>)",v) is not defined in $Emit("SetValue(std  signal_slot_vector.cpp:41:
*** Interpreter error recovered ***

Another variant without std does not work too:

//root cern threads
#include "TThread.h"
#include <Riostream.h>

//root cern general
#include <TApplication.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>

//root cern GUI
#include <TGClient.h>
#include <TGButton.h>
#include <TRootEmbeddedCanvas.h>
#include "TGFrame.h"
#include "TGTab.h"
#include <TQObject.h>
#include <RQ_OBJECT.h>


using namespace std;


class MyClass
{
   RQ_OBJECT("MyClass")
private:
   vector<Int_t> fValue_vec;
public:
   MyClass() { fValue_vec.resize(5); }
   Int_t GetValue() const { return fValue_vec; }
   void  SetValue(vector<Int_t>); // *SIGNAL*
};

void MyClass::SetValue(vector<Int_t> v)
{
    for (int i = 0; i < fValue_vec.size(); ++i)
    {
        fValue_vec[i] = v[i];
    }

    Emit("SetValue(vector<Int_t>)",v);
}

void signal_slot_vector_no_std()
{
    MyClass *objA = new MyClass();
    MyClass *objB = new MyClass();
    objA->Connect("SetValue(vector<Int_t>)","MyClass",objB,"SetValue(vector<Int_t>)");

    vector<Int_t> vec_a;
    vec_a.push_back(1);
    vec_a.push_back(2);
    vec_a.push_back(3);
    vec_a.push_back(4);
    vec_a.push_back(5);

    objA->SetValue(vec_a);

}

Output:

Error: Function Emit("SetValue(vector<Int_t>)",v) is not defined in current scope  signal_slot_vector_no_std.cpp:41:
*** Interpreter error recovered ***

P.S. my root is

ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on linuxx8664gcc)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010

My system is

Scientific Linux release 7.3 (Nitrogen)

Thank you in advance.

Hi,

Try like this:

//root cern threads
#include "TThread.h"
#include <Riostream.h>

//root cern general
#include <TApplication.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>

//root cern GUI
#include <TGClient.h>
#include <TGButton.h>
#include <TRootEmbeddedCanvas.h>
#include "TGFrame.h"
#include "TGTab.h"
#include <TQObject.h>
#include <RQ_OBJECT.h>


//using namespace std;


class MyClass
{
   RQ_OBJECT("MyClass")
private:
   std::vector<Int_t> fValue_vec;
public:
   MyClass() { fValue_vec.resize(5); }
   std::vector<Int_t> GetValue() const { return fValue_vec; }
   Int_t GetValue(Int_t index) const { return fValue_vec[index]; }
   void  SetValue(std::vector<Int_t>*); // *SIGNAL*
};

void MyClass::SetValue(std::vector<Int_t> *v)
{
    for (int i = 0; i < fValue_vec.size(); ++i)
    {
        fValue_vec[i] = v->at(i);
    }

    Emit("SetValue(std::vector<Int_t>*)", (Long_t)v);
}

void signal_slot_vector()
{
    MyClass *objA = new MyClass();
    MyClass *objB = new MyClass();
    objA->Connect("SetValue(std::vector<Int_t>*)","MyClass",objB,"SetValue(std::vector<Int_t>*)");

    std::vector<Int_t> vec_a;
    vec_a.push_back(1);
    vec_a.push_back(2);
    vec_a.push_back(3);
    vec_a.push_back(4);
    vec_a.push_back(5);

    objA->SetValue(&vec_a);
    std::vector<Int_t> ret = objB->GetValue();
    for (int i = 0; i < ret.size(); ++i) {
//        cout << "objB->GetValue() : vector[" << i << "] = " << ret[i] << endl;
        cout << "objB->GetValue(" << i << ") = " << objB->GetValue(i) << endl;
    }
}

i.e. use a pointer to the std::vector to the Emit() function. It gives:

C:\Users\bellenot\rootdev\Forum>root -l
root [0] .x signal_slot_vector.C
objB->GetValue(0) = 1
objB->GetValue(1) = 2
objB->GetValue(2) = 3
objB->GetValue(3) = 4
objB->GetValue(4) = 5
root [1]

Cheers, Bertrand.

1 Like

Dear Bertrand,
Thanks! Thanks! Thanks!
It works.
You helped me again)

1 Like

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