Hi,

I need to send some arguments in a callback but it doesn't seem to be possible in an object. Basically, I saw that in the callback tutorial:

void function(tgui::Gui& gui);
button->bindCallback(std::bind(function, std::ref(gui)), tgui::CheckBox::Checked | tgui::CheckBox::Unchecked);

And I wanted to do that:

p.button->bindCallback(std::bind(&AddObjectDialog::CloseAndSaveCallback, p.object), this, tgui::Button::LeftMouseClicked);

But it doesn't seem to be possible, I get the following error:

'tgui::CallbackManager::bindCallback' : no overloaded function takes 3 arguments

Thanks.
« Last Edit: 17 June 2015, 18:00:58 by Dexario »

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Function Callbacks: call an object's method with args
« Reply #1 on: 17 June 2015, 18:01:44 »
Since the CloseAndSaveCallback is a function inside a class, it looks like this for the compiler:
void CloseAndSaveCallback(const AddObjectDialog* this, Obj obj)

std::bind takes the following parameters: the function and the parameters. Since "this" is always the first parameter, it should always be before the other parameters in the bind call. The bind call needs to look like this:
std::bind(&AddObjectDialog::CloseAndSaveCallback, this, p.object)

The bindCallback function has one version where the first parameter is the function and the second one is the 'this' pointer, but this is actually only a shortcut so that you don't have to call std::bind yourself. But if you need a function that has parameters then you must use the normal bindCallback function. So the line should look like this:
p.button->bindCallback(std::bind(&AddObjectDialog::CloseAndSaveCallback, this, p.object), tgui::Button::LeftMouseClicked);

Re: Function Callbacks: call an object's method with args
« Reply #2 on: 17 June 2015, 18:05:27 »
Thanks !