Listbox signals
« on: 10 May 2018, 16:31:37 »
I have some problems going from 0.7 to 0.8 regarding the signals and in particular the old connectEx functions.
In example I had a function called when an item in a list box is selected. The function passed to the old connectEx used a parameter Callback that reported the selectedItemId.

How can I get the same with the new signal system?

Sorry for the noob question, but I have not found any tutorial...

*

texus

  • *****
  • 1254
    • View Profile
    • Texus's Blog
Re: Listbox signals
« Reply #1 on: 10 May 2018, 16:47:39 »
It's badly documented, but the ItemSelected trigger has 1 or 2 strings as optional parameters. So you can just add an sf::String (or std::string, with or without const&) parameter to the callback function then it will be called with the selected item. If items in the list box aren't unique then the extra string parameter will contain the id (optional second parameter passed to addItem).

void f1() { std::cout << "Item selected" << std::endl; }
void f2(const sf::String& item) { std::cout << "Item selected: " << item.toAnsiString() << std::endl; }
void f3(std::string item, std::string id) { std::cout << "Item selected: " << item << " with id: " << id << std::endl; }
listBox->connect("ItemSelected", &f1);
listBox->connect("ItemSelected", &f2);
listBox->connect("ItemSelected", &f3);

Re: Listbox signals
« Reply #2 on: 10 May 2018, 16:57:02 »
Ok, thank, this worked perfectly!

Re: Listbox signals
« Reply #3 on: 11 May 2018, 13:13:20 »
Hi again, I have a problem with the listbox, maybe I'm doing something wrong. My program crashes when I call listbox->removeAllItems() and points to ListBox.cpp line 916 (   m_items[m_hoveringItem].setColor(m_textColorCached);)

I'm using VC 2015 and shared libs:

Code: [Select]
void Test()
{
sf::RenderWindow window({ 600,600 }, "test");
tgui::Gui gui(window);
tgui::ListBox::Ptr listbox = tgui::ListBox::create();
gui.add(listbox);

listbox->addItem("uno");
listbox->addItem("due");
listbox->addItem("tre");

listbox->connect("MousePressed", [=]() { listbox->removeAllItems(); });

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();

gui.handleEvent(event);
}

window.clear();
gui.draw();
window.display();
}
}

*

texus

  • *****
  • 1254
    • View Profile
    • Texus's Blog
Re: Listbox signals
« Reply #4 on: 11 May 2018, 13:40:16 »
This is the problem with developing on linux, it just runs no matter what happens :). My pc happily continues running without issues even after invalid memory was accessed like in this code. Although I couldn't reproduce it here, I think I know what the issue was and it should be fixed in the latest version.

Re: Listbox signals
« Reply #5 on: 11 May 2018, 13:41:08 »
good  ;)