The Red Penguin

Add a component ID and converting between ints and strings


Thursday 6 August 2020

In the last post, we got as far as creating a button listener, which allowed us to trigger a function when the user clicks on one of the buttons in our playlist. If the user clicks on a button it prints out a message.

Now the problem is we have no way currently of identifying the button. So what do we normally do? Well, we look at the address.

We need to convert the ID number from an integer in a string to do this.

1. In the refreshComponentForCell function in PlaylistComponent.cpp add
juce::String id{std::to_string(rowNumber)};
btn->setComponentID(id);
This changes the integer into a standard string and then passes the variable back correctly.

2. We need to convert from a text string back to an integer to get the index in the track list vector. So in PlaylistComponent::buttonClicked we add:
int id = std::stoi(button->getComponentI().toStdString());

This shows we start with a JUCE string, we convert to a standard string using toStdString() and then stoi converts it to an integer.

Then in buttonclicked we can do something like
std::cout << trackTitles[id] << std::endl;
to get the name of the track with the ID index.

*** I didn't really understand a lot of this!