Callbacks with the ComboBox

Started by Charsmud, 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 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:

comboBox->bindCallbackEx(function, tgui::ComboBox::ItemSelected);

comboBox->connectEx("ItemSelected", function);

In both cases the function looks like this:
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.


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

void resolutionCallback(const tgui::Callback& callback);
void GuiOptions::resolutionCallback(const tgui::Callback& callback)
selectedResolution = callback.text;
std::cout << selectedResolution << std::endl;


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


In case you haven't figured it out yourself yet by now, the code you need is:
Code (cpp) Select
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.