PDA

View Full Version : Dynamic label to right of slider resizes slider



zenzero-2001
2nd October 2013, 21:01
Hi all

I have a dumb question, but this doing my head in (as layouts generally do). I'm pretty sure the solution is straight forward, I'm just not seeing it.

Anyway, I have a dialog with three horizontal sliders (they are actually custom sliders, but I don't think it is relevant). Each slider is for picking a color value from 0-255 (for red, green and blue respectively). To the right of each slider I have a label to display its value. The sliders have a horizontal size policy of expanding so that when the dialog is resized horizontally so are the sliders. The problem is when the value in a label changes between 1, 2 and 3 characters in length it affects the length of the slider. I wish the sliders to remain unchanged in length when the label value changes.

I was thinking that I need a minimum size for the label that can hold three characters, but I don't think this will take into consideration variations in font size?

The sliders also each have a label to their left, R, G and B, respectively. I currently have the labels and the sliders in a grid layout.

Thanks for reading.

anda_skoa
3rd October 2013, 09:18
What you could do is create a QLabel Subclass and reimplement sizeHint() or minimumSizeHint() such that is uses QFontMetrics of the current font to determine the length of three digits.

Cheers,
_

zenzero-2001
3rd October 2013, 10:37
That's a good idea, I will try that. Thanks.

Added after 25 minutes:

Yeah, that worked. So easy. Thanks! I'm a bit out of practice with Qt but it's coming back to me now.