Overloading text from value for slider


I have one rotary slider which I want to show custom value (changing units based on the value). I’m not sure the correct way to overload textFromValueFunction(double value).

I tried making a new slider class that inhereits from Slider:

class DelaySlider : public Slider


	juce::String getTextFromValue(double value)
		if (value >= 1000) {
			return juce::String(value/1000.0) + "s";
			return juce::String(value) + "ms";


And then using a DelaySlider instead of Slider for the appropriate one, but it doesn’t work. Debugging shows that textFromValueFunction isn’t called.

Am I approaching this the correct way?


EDIT: I got the function name wrong. I’m an idiot. Still interested if there’s a better way to do the overload than creating a new class that is a child of Slider, though?

The Slider class has 5 public std::function<> members that you can assign to respond to:

  • onValueChange
  • onDragStart
  • onDragEnd
  • valueFromTextFunction
  • textFromValueFunction

Here’s an example I have laying around that lets a slider control a 32-bit unsigned int:

  controllerDataSlider->textFromValueFunction = [] (double val) {
       auto scaled = static_cast<unsigned int>(((0xffffffff) * val)  + 0.5f);
       return String(scaled);
   controllerDataSlider->valueFromTextFunction = [] (const String& text) {
        int64 textValue = text.getLargeIntValue();
        return textValue / static_cast<double>(0xFFFFFFFF);

If you want to override a method, add the override keyword in your declaration (header), like:

juce::String getTextFromValue(double value) override //...

In this case the compiler will tell you, if your method doesn’t override because of spelling errors or the original is not virtual.
Since the override keyword was added later, code without it is still legit, but as soon as you used it once in a class, you get warnings for all methods, that override but don’t have the keyword…

But for this particular case, @bgporter’s answer is the more modern approach, since the lambdas are even propagated to a slider, if you use the AudioProcessorValueTreeState::SliderAttachment.