Introduction to signals

TGUI 0.7 is no longer supported, use a newer TGUI version instead.

Connecting to signals

You use the 'connect' function to connect your signal handler function to a specific signal. The first parameter is the name of the signal, this is the trigger on which the callback should be send. This name is case-insensitive, so it does not matter if you write "clicked" or "Clicked". The second parameter is the function that you want to be called when the signal is send.

Here is an example of letting a function get called when a button has been pressed.

void function()
    std::cout << "Button pressed" << std::endl;

button->connect("pressed", function);

You could also use lambda functions of course if your function is very small. This is all the code you need to close the window when your quit button gets pressed:

quitButton->connect("pressed", [&](){ window.close(); });

Connecting multiple signals at once

The Checkbox class has a separate "Checked" and "Unchecked" signal. You may want the same function to handle this, so it would be a waste to bind them separately and thus duplicate code. So you can connect to multiple signals at once by putting a space between them in the string.

checkbox->connect("checked unchecked", function);

Binding parameters

So far the functions did not have any parameters. But the function may e.g. need access to the Gui object. This can be solved by just passing the needed parameters to the connect function.

void function(tgui::Gui& gui, int i) {}
picture->connect("clicked", function, std::ref(gui), 5);

Connecting member functions

Member functions are special. You must first understand a technical detail in c++ in order to bind them.

Suppose you have the following class:

class MyClass
    void function(int i);

That function takes one parameter, right? Wrong.
That function takes 2 parameters. Behind the scenes the function looks like this:

void function(MyClass* this, int i);

That is where your 'this' pointer is coming from.

Now that you know this, you can connect the function.

int i = 2;
MyClass myClassInstance;

// &MyClass::function is the function pointer here
// and &myClassInstance is the value for 'this'
button->connect("pressed", &MyClass::function, &myClassInstance, i);

Binding function calls

Finally there is an awesome thing that you can do with std::bind. You can have a function called and then pass its return value as argument of the signal handler function. In the code below, func2 will be called when the button is pressed, with the value of the slider as parameter.

int getNum() { return 5; }

void func1(int i);
void func2(int value);

picture->connect("clicked", func1, std::bind(getNum));
button->connect("pressed", func2, std::bind(&tgui::Slider::getValue, slider));

Disconnecting signals

The connect function returns an id, which can be used to diconnect that function.

unsigned int id = button->connect("pressed", function);

When multiple signals are bound at once, only the id of the last signal is returned. The signal before that is guaranteed to have an id of one less than the returned value.

unsigned int id = checkbox->connect("checked unchecked", function);
button->disconnect(id-1); // disconnect "checked" signal
button->disconnect(id); // disconnect "unchecked" signal

It is also possible to disconnect all function that connected to a specific signal or even all signals.


Receiving information from widget

The examples so far just called a function when something happens. But what if you want to receive information about the callback as parameter to your function? There are two ways to do this, you can use the new system with unbound optional parameters or you can use the older system with the Callback class.

Unbound optional parameters (limited compiler support)

connectEx function (alternative legacy system)