Changing the label position of a slider


#1

im using a lookandfeel class to display different graphics to the screen, i can use a lookandfeel on a label then return that to use with my slider but i cant seem to change the position, i can only use Slider::TextEntryBoxPosition, how can i adjust it by a few pixels?


#2

Your best bet is to subclass the slider component and overide the paint method. If you’re lucky, you might be able to call the parent methods in a way that you want. eg select NoTextBox and paint the parent slider, then call paint on the text box manually after adjusting its position.

read the code of the slider class and it should be straightforward.

good luck,


#3

paint isnt virtual though so theres no way to override it


#4

Component::paint(Graphics&) is virtual. you can overide this in your subclass.


#5

i wouldnt be able to inherit from slider and override the paint as its not virtual, i could inherit from Component and overide its paint method but then i lose out on the slider funcitonality and wouldnt be able to seamlessly integrate my new slider into an existing project that works with sliders.


#6

well, if Slider inherits from Component, and Component::paint is virual, then Slider::paint is virtual. that’s how it works.

so you’re good to simply override Slider::paint in your subclass.


#7

As hugh says, you can’t un-virtualise a method but I don’t think its paint() you need to override, its resized(). This is complicated by the fact that Slider uses a pimpl which you won’t have access to.

If you need fine control over things like this then it might be better to just create your own class, based on the Slider code. Alternatively perhaps Jules could add a look and feel method that positions the Label? Then you could override that and do what you want, it may complicate things a bit too much though as you’d probably then also need to be responsible for the positioning of the actual slider bit. That changes greatly depending on the type of slider used.


#8

[quote=“hugh”]well, if Slider inherits from Component, and Component::paint is virual, then Slider::paint is virtual. that’s how it works.

so you’re good to simply override Slider::paint in your subclass.[/quote]
i didnt realise that, i always thought if you wanted to further derive a method that every function in the chain had to be declared virtual

[quote=“dave96”]As hugh says, you can’t un-virtualise a method but I don’t think its paint() you need to override, its resized(). This is complicated by the fact that Slider uses a pimpl which you won’t have access to.

If you need fine control over things like this then it might be better to just create your own class, based on the Slider code. Alternatively perhaps Jules could add a look and feel method that positions the Label? Then you could override that and do what you want, it may complicate things a bit too much though as you’d probably then also need to be responsible for the positioning of the actual slider bit. That changes greatly depending on the type of slider used.[/quote]
ah yes i see resized in slider is responsible for setting the boudns of the valuebox.

in the end i created an ordinary slider, set a look and feel, then created a label that my slider listens to. was a bit roundabout but it worked.


#9

I’m hitting the same problem. I need a new TextEntryBoxPosition, and without it, it looks like subclassing the whole slider class would be needed.

I’ll try your workaround, unless Jules, would you consider: TextBoxCentered?

Bruce


#10

I think the better way is to create your own Label, and use attachToComponent().

Then just override Label::componentMovedOrResized() with soemthing like:

[code]
void componentMovedOrResized (Component& component, bool /wasMoved/, bool /wasResized/)
{
const Font f (getLookAndFeel().getLabelFont (*this));

    setSize (component.getWidth(), roundToInt (f.getHeight()));  
    setTopLeftPosition (component.getX(), component.getY() - getHeight());
}[/code]