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)};This changes the integer into a standard string and then passes the variable back correctly.
btn->setComponentID(id);
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!
More posts in cpp
- Add a component ID and converting between ints and strings
- Implement a play button and add a listener
- Implement paintRowBackground and paintCell
- Add a vector to store a list of files
- Add a TableListBox
- Create a PlaylistComponent in the Projucer project
- Threads
- Implement a timer
- Add getPosition and setPosition functions
- Refactor DJAudioPlayer to use app-scope formatManager
- Draw the thumbnail
- Hook up the load button to trigger the AudioThumbnail load
- The AudioThumbnail class in the API
- Creating a new component - WaveformDisplay
- Implementing drag and drop triggers
- Use a MixerAudioSource to play more than one file at a time
- Implement the listener interfaces to DeckGUI
- Creating a DeckGUI class
- setPosition control
- Implementing setGain and setSpeed
- Add audio playback functionality
- Writing the DJAudioPlayer class
- Creating a new JUCE class with Projucer
- Refactoring our code
- Using ResamplingAudioPlayer to implement variable speed playback
- Add stop, start and volume functionality
- Add a file chooser
- Audio file playback in JUCE
- Realtime sound synthesis in JUCE
- Adding a slider listener
- Introduction to event listeners
- Macros
- Inheritance
- Adding a GUI widget to the JUCE app
- Introduction to JUCE