Slider text value other than Slider value


#1

Hello,
probably it’s stupid question, but I’ve spent last 2 hours to find the solution, with no result.
Everybody tell to use getTextFromValue(); or getValueFromText(); But nobody tells how exactly use it?
Those methods just return value, but I don’t want to return anything. I want to set text value other than Slider value. What I mean: my Slider range is from 0.0 to 1.0. But I want text box to display from 20 Hz to 20000 Hz.
Please could anyone help me?


#2

If you want the slider to format the text it displays you can set the lambdas juce::Slider::textFromValueFunction and juce::Slider::valueFromTextFunction for your slider.

Using your range for a basic example:

mySlider.textFromValueFunction = [](double value)
{
    double freq = (value * (20000.0 - 20.0)) + 20.0;
    return juce::String(freq) + "Hz";
};

mySlider.valueFromTextFunction = [](const String &text)
{
    double freq = text.removeCharacters("Hz").getDoubleValue();
    return (freq - 20.0) / (20000.0 - 20.0);
};

The valueFromTextFunction can allow the user to use your units & formatted range when they enter in values. In this case typing 10010Hz into the text box would display that frequency while setting the slider’s actual value to 0.5


#3

Hello,
great thanks for reply and for help. Probably first I should try your code and then ask additional question. But my first impression is: When I do that code I have lambda functions stored in mySlider.textFromValueFunction and in mySlider.valueFromTextFunction, but both of those lambda functions return some value. So anytime I call mySlider.textFromValueFunction(someValue); and mySlider.valueFromTextFunction("some text"); they return values. But how to put those values in the text box. That is what I am asking for.


#4

textFromValueFunction returns a juce::String, which the slider will put into its text box


#5

Great man, it works. Thanks


#6

But you should mention that after I create that lambda function I should use it by calling mySlider.textFromValue(someValue);
Because at first I tried to call mySlider.textFromValueFunction(someValue); and nothing happened, just error :slight_smile:


#7

Just wanted to point you to a more readable approach using jmap

auto freq = jmap (value, 20.f, 20000.f);

auto v = jmap (freq, 20.f, 20000.f, 0.f, 1.f);

If you’re having trouble to compile, make sure that all arguments have the same type, since these templated functions are picky and don’t do implicit casts.

Another option is, let the range of the slider take care of it, but that would mean, that you set the Range to Range<float> (20.f, 20000.f) and all is sorted…


#8

Hey everybody,
It’s old subject, but now I’ve found one more issue.
When I set:

mySlider.textFromValueFunction = [](double value)
{
     double freq = (value * (20000.0 - 20.0)) + 20.0;
     return juce::String(freq) + “Hz”;
};

Slider works great but unfortunately if I have editable textBox, and I edit text I get crazy values. It looks like that lambda calculates my edited value also. How to prevent that?


#9

Look at @TonyAtHarrison’s answer, he gave you both needed functions.


#10

Yes I’ve already figured it out, thanks.