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.
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:
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.
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.
Member functions are special. You must first understand a technical detail in c++ in order to bind them.
Suppose you have the following class:
That function takes one parameter, right? Wrong.
That function takes 2 parameters. Behind the scenes the function looks like this:
That is where your 'this' pointer is coming from.
Now that you know this, you can connect the function.
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.
The connect function returns an id, which can be used to diconnect that 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.
It is also possible to disconnect all function that connected to a specific signal or even all signals.
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)