Topic: Simple problem with std::bind and OnClick function

Hi there!

So, I've just started using SFGUI in my SFML project and I'm struggling to understand how everything works. I've been reading through the hello world tutorial and some of the examples the library itself offers in it's folders.

I am now trying to create a simple button that does something when it detects a left click, but I can't even get that to work properly. Apparently the button variable is not defined in my OnClick method, and if I try to send button using the parameters of std::bind I get an error.

In most examples of the library, this problem is solved by using this as the second parameter, right after the function name, but that doesn't seem to work for me.

How should this be done?

My code:

void pressEnter(){
    button->SetLabel("Tocado");
}

int main(){
    
    sfg::SFGUI sfgui;
    
    sf::VideoMode video_mode( 800, 600 );
    sf::RenderWindow rwindow( video_mode, "In-joked - Menu Principal");
    sf::Event event;
    
    auto button = sfg::Button::Create("Status");
    button->GetSignal(sfg::Button::OnLeftClick).Connect(
            std::bind(&pressEnter, button));
    
    ...
}

Re: Simple problem with std::bind and OnClick function

Just use C++11/14 lambda expressions... It is universally agreed upon that with lambdas there is almost no need for std::bind any longer. Read up on lambdas on the internet, you will learn to love them. They will make SFGUI event handling code also look much cleaner and improve locality (i.e. you write the code where you actually need it).

Re: Simple problem with std::bind and OnClick function

binary1248 wrote:

Just use C++11/14 lambda expressions... It is universally agreed upon that with lambdas there is almost no need for std::bind any longer. Read up on lambdas on the internet, you will learn to love them. They will make SFGUI event handling code also look much cleaner and improve locality (i.e. you write the code where you actually need it).

Thanks a lot, I finally got it working. Guess I'll start using lambdas then!