Connecting multiple signals

Started by UnderPlayer, 29 July 2019, 21:06:57

UnderPlayer

Hello there,

I just wondered, in previous versions you were able to write code like

Code (cpp) Select
my_checkbox->connect("checked unchecked", [](bool status){ std::cout << status << '\n'; });

I had a look into the code and found that - since version 0.8 - the connect function got "simplified" to

Code (cpp) Select

        const unsigned int id = getSignal(toLower(signalName)).connect([f=std::function<void(const Args&...)>(handler),args...](){ f(args...); });
        m_connectedSignals[id] = toLower(signalName);
        return id;


or one of its sibblings. So, when writing the code mentioned above you will get an error that reads "There is no signal called 'checked unchecked'". Why did that behaviour get changed? What would be the best way to accomplish the previous behaviour?

Lars

UnderPlayer

Just a quick update, I got around this by creating the following helper function, which connects a functor to multiple signals. Maybe you've got some better ideas for implementations :).

Code (cpp) Select
template<class Func, class ...Args>
static void connect_multiple(
const std::string& signals,
tgui::Widget::Ptr widget,
Func&& functor,
Args... args)
{
std::string signal;
std::istringstream iss(signals);
while (std::getline(iss, signal, ' '))
{
widget->connect(signal, functor, args...);
}
}

texus

The functionality wasn't removed, just changed. There is a connect function that takes an initializer_list as parameter, so you now have to write the following:
Code (cpp) Select
my_checkbox->connect({"checked", "unchecked"}, [](bool status){ std::cout << status << '\n'; });

UnderPlayer

Ah, my bad. I overlooked that somehow.

texus

I just remembered that since v0.8.4 you can also just use the "Changed" signal instead of connecting to both checked and unchecked.