cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the position of the indicator in a slider?

Sandy.Dahl
Associate II

Hi @Martin KJELDSEN​ !

I'm working on a slider and I have two questions for you. =)

  • Is there a way to get the position of the indicator in a slider? I am showing the value of the slider in a textarea above the slider. I want to move the text if the indicator is moving. I would like to do something like this:

sliderValueText.moveTo(sliderBar.getIndicatorX(), SLIDER_TEXT_POS_Y);

  • I think I've found a bug. When doing an interaction in the designer that is supposed to move a widget when slider value is changed it generates a comment in code:

//Interaction1

//When slider1 value changed move sliderValueText

//Set position x:71 and y:71 on sliderValueText

sliderValueText.moveTo(281,71);

But as you can see, the comment is not correct the x-position should be 281 and not 71. Just a minor thing I came across while testing in the designer.

Superthankful as always for help!

2 REPLIES 2
Martin KJELDSEN
Chief III

Hi Sandy,

Re: your bug, could you try this in the latest release 4.12.3 before we go any further with that?

Re your feature request. This method allows you to associate a callback with a handler that will be called whenever the value of the slider changes. You could use that value to move your text area along with it. Notice that here you're getting the "value" and not the x coordinate, so you'll have to calculate it.

/**
     * @fn void Slider::setNewValueCallback(GenericCallback< const Slider&, int >& callback)
     *
     * @brief Associates an action to be performed when the slider changes its value.
     *
     *        Associates an action to be performed when the slider changes its value.
     *
     * @param callback The callback to be executed. The callback will be given a reference to
     *                 the Slider and the current value of the slider.
     *
     * @see GenericCallback
     */
    void setNewValueCallback(GenericCallback< const Slider&, int >& callback)
    {
        newValueCallback = &callback;
    }

If you want to embed this feature inside your custom Slider, then simply implement your change in:

void Slider::updateIndicatorPosition(int16_t position)
{ 
    ...
 
    // Communicate the new value if a listener is registered
    if ((newValueCallback != 0) && newValueCallback->isValid())
    {
        newValueCallback->execute(*this, currentValue);
    }
 }

You'll see at the bottom there's some code to execute the callback imentioned before with the currentValue. Somewhere in this method, you'll move your textarea to follow the "x position" (For HORIZONTAL) and not the currentValue which relates to the actual value range defined by the user.

It should be apparent from the source code of the Slider how to get the x position.

/Martin

Hi!

Thank you for the answers. 🙂

I tried generating code in the latest release (4.12.3) of the designer and the issue is still there unfortunately. The comment is still wrong. This is not an issue really, just thought I should share it with you.

Regarding the feature I was hoping it was already implemented. I'll try calculating something of my own. 🙂

Kind regards

Sandy