[Qt-interest] Slider with labels
Esben Mose Hansen
kde at mosehansen.dk
Tue Jun 23 09:45:59 CEST 2009
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
More information about the Qt-interest-old
mailing list