Callbacks with the ComboBox
« on: 29 December 2014, 03:55:19 »
Hello!  I am attempting to work with callbacks with the ComboBox.  How do I know what is selected in the box?

*

The Illusionist Mirage

  • *
  • 2
  • My Life = C++ & Computers
    • View Profile
    • fleptic

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Callbacks with the ComboBox
« Reply #2 on: 29 December 2014, 12:02:53 »
The question is a bit unclear.

If your goal is to find out what the currently selected item is, then the above answer is correct.

If you however want a callback when the selected item changes, then do the following:

v0.6:
Code: [Select]
comboBox->bindCallbackEx(function, tgui::ComboBox::ItemSelected);
v0.7-dev:
Code: [Select]
comboBox->connectEx("ItemSelected", function);
In both cases the function looks like this:
Code: [Select]
void function(const tgui::Callback& callback)
{
    // callback.text contains the newly selected item
}

For more information about how the callbacks work, see the tutorials.

PS: As you can see it is best to mention which tgui version you are using. I know it is unlikely that you are using v0.7-dev, but I still have to give the information as well just in case.

Re: Callbacks with the ComboBox
« Reply #3 on: 31 December 2014, 21:12:07 »
Attempting to bind the callback gives me the following error:

1>e:\gamedev\c++\byte\byte\guioptions.cpp(59): error C3867: 'GuiOptions::resolutionCallback': function call missing argument list; use '&GuiOptions::resolutionCallback' to create a pointer to member

Code: [Select]
void resolutionCallback(const tgui::Callback& callback);
Code: [Select]
void GuiOptions::resolutionCallback(const tgui::Callback& callback)
{
selectedResolution = callback.text;
std::cout << selectedResolution << std::endl;
}

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Callbacks with the ComboBox
« Reply #4 on: 31 December 2014, 21:26:02 »
It is because you are binding a member function instead of normal function.

I don't have time to write a full answer right now, but it is mentioned in the tutorials https://tgui.eu/tutorials/v06/callback-system/.

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Callbacks with the ComboBox
« Reply #5 on: 31 December 2014, 23:18:14 »
In case you haven't figured it out yourself yet by now, the code you need is:
comboBox->bindCallbackEx(&GuiOptions::resolutionCallback, this, tgui::ComboBox::ItemSelected);

I'm assuming that you are putting this code inside a function from GuiOptions, based on the error. Otherwise the 'this' should be replaced with the pointer to the class instance.