Signals, unbound parameter

Started by RitoonL, 01 June 2020, 16:58:51

RitoonL

Hello,

I'm trying to connect the SizeChanged signal from a child window.

I followed the tutorial and tried to do a simple thing to test :


Code (cpp) Select
void Ch_window::_test()
{
    cout<<"size changed"<<endl;
}

void Ch_window::channels_window (sf::RenderWindow &window, tgui::Gui& gui)
{
    auto channels = tgui::ChildWindow::create();
    channels->setTitle("Channels");
    channels->setTitleAlignment(tgui::ChildWindow::TitleAlignment::Left);
    channels->setResizable(true);//(tgui::ChildWindow::setResizable=true);
    channels->getRenderer()->setBackgroundColor(sf::Color::Black);
    channels->getRenderer()->setTitleBarHeight(15);
    channels->getRenderer()->setTitleColor(sf::Color::White);
    channels->getRenderer()->setTitleBarColor(sf::Color(20, 30, 60));
    channels->getRenderer()->setBorders(1);
    channels->getRenderer()->setMinimumResizableBorderWidth(3);
    channels->getRenderer()->setBorderColor(sf::Color(20, 30, 60));
    channels->connect("SizeChanged", _test);
}


this give me an error when i run the program

terminate called after throwing an instance of 'tgui::Exception'
  what():  Signal 'SizeChanged' could not provide data for unbound parameters.

i search about thes unbound parameters but did not find much. what i understand is that SizeChanged signal wants to give something to my function but i did not set arguments to my fuction to receive it.

is it newsize ? my _test function should havec a vector2f as argument ? ***edit*** seems not, void _test(sf::Vector2f new_size) did not changed anything.

thanks by advance,

RitoonL


texus

#1
The problem is that _test() is a member function.
Unlike a free function (a function that isn't part of a class), a member function needs to know what object it is called on. If you access member variables or call other member functions then the code needs to know on which object it needs access these members (since you may create multiple Ch_window objects).

Although you don't write any parameters, the c++ compiler will generate a function that is equivalent to the following:
Code (cpp) Select
void _test(Ch_window* this);

That is where the "this" object comes from that you can use within a class, behind the scenes it is a parameter passed to all your member functions.
So the function does have a parameter, which is what the connect call is complaining about (it doesn't know what value it has to pass to your this pointer).

Since TGUI needs to know on which object to call the _test function, you have to give it a pointer to the object. Since you are calling the connect function inside a member function of Ch_window and you obviously want to call the _test function on the same object, the pointer to the object is literally your "this" value. You also have to change the function name for it to work properly:
Code (cpp) Select
channels->connect("SizeChanged", &Ch_window::_test, this);

Alternatively, you can use a lambda function, which is similar to what TGUI will internally do when you write the above:
Code (cpp) Select
channels->connect("SizeChanged", [this]{ _test(); });

RitoonL