Connecting multiple signals

  • 4 Replies
  • 46 Views
Connecting multiple signals
« on: 29 July 2019, 21:06:57 »
Hello there,

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

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

        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

Re: Connecting multiple signals
« Reply #1 on: 29 July 2019, 21:23:48 »
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 :).

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

  • *****
  • 1414
    • View Profile
    • Texus's Blog
Re: Connecting multiple signals
« Reply #2 on: 29 July 2019, 22:05:50 »
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:
my_checkbox->connect({"checked", "unchecked"}, [](bool status){ std::cout << status << '\n'; });

Re: Connecting multiple signals
« Reply #3 on: 29 July 2019, 22:40:57 »
Ah, my bad. I overlooked that somehow.

*

texus

  • *****
  • 1414
    • View Profile
    • Texus's Blog
Re: Connecting multiple signals
« Reply #4 on: 30 July 2019, 08:01:47 »
I just remembered that since v0.8.4 you can also just use the "Changed" signal instead of connecting to both checked and unchecked.