Ever needed a widget which allows to select a double value out of a set of given values? You could use a combo box. Not very nice… Sliders are much more convenient to use but QSlider (which comes with Qt) can only be used to let the user select a value out of some equidistant spaced values. E.g. 1, 2, 3 … 10. What if you want the user to choose from say 5, 10, 12.34, 23.1, 30, 38? You have to write your own widget… Here’s what i came up with.
It looks like this:
I subclassed QWidget and implemented paint-, mouseMove- and mousePressEvent() and added some signals and slots. You just have to set the values and connect to the valueChanged(double) signal.
Use it like this:
QList<double> values;
values << 5 << 10 << 12.24 << 23.1 << 30 << 38;
NonEquidistantSlider* slider = new NonEquidistantSlider(this);
slider->setValues(values);
connect(slider,SIGNAL(valueChanged(double)),
this,SLOT(setValue(double));
You can download und use my code as you like: NonEquidistantSlider.h NonEquidistantSlider.cpp

This is very nice! I think I’ll try that one with qtruby at some time.
Comment by Wojtek — October 19, 2008 @ 6:45 pm