[Qt-interest] Slider with labels

e.jokisch at orange-moon.de e.jokisch at orange-moon.de
Tue Jun 23 12:36:25 CEST 2009


Hi,
for what is the reason that you do not simply put the labels above the
ticks (if it's a horizontal slider)?
Eckhard
You could put all this into a groupbox for layouting etc.
> Hi,
>
> I need a slider with a label (mon-sun) at each tickmark. Since Qt does not
> provide this, I have tried to roll my own, inheriting from QSlider. This
> worked well visually, but the mouse control is off because the original
> QSlider
> class thinks the slider is still at the original position. That position
> is
> different to make room for the labels.
>
> Reimplementing the mouse controls is quite a lot of code, but I fail to
> see
> any way to tell QSlider about the correct positions. The code gets the
> subrect
> from QStyle::subControlRect, which doesn't seem to provide much in the way
> of
> hooks. Of course, if I could override initStyleOption() I could probably
> cheat
> and provide the old rectangle, but alas, this function is not virtual.
>
> Any hints/helps/ideas?
>
> Some details:
>
> I my paintEvent I simply set up the QStyleOptionSlider
>
>   QStyleOptionSlider slideropt;
>   initStyleOption(&slideropt);
>   int first_label_offset = m_labels.empty() ? 0 :
> slideropt.fontMetrics.width(m_labels[0])/2;
>   int last_label_offset = m_labels.empty() ? 0 :
> slideropt.fontMetrics.width(m_labels.last())/2;
>   slideropt.rect.setX(slideropt.rect.x()+first_label_offset);
>   slideropt.rect.setWidth(slideropt.rect.width()-first_label_offset-
> last_label_offset);
>   slideropt.subControls = QStyle::SC_SliderGroove |
> QStyle::SC_SliderHandle;
>
> Then draw the labels, and then call the
>
>   // Draw base slider, without ticks
>   style()->drawComplexControl(QStyle::CC_Slider, &slideropt, &p, this);
>
> Other than that, sizeHint() is simply set as
>
>
> --
> kind regards, Esben
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>





More information about the Qt-interest-old mailing list