use a created vector as range for QDoubleSpinBox and QSlider
Question
I've created a vector v with 128 elements from -3000 to 3000 and I want to associate it to a QDoubleSpinBox
and a QSlider
, because dividing 6000 to 128 and setting the QDoubleSpinBox
we have always the round problem. So can we set the range and the stepsize both to a QDoubleSpinBox
and QSlider
with a vector like this?
std::vector<double> v(128);
for (int i = 0; i < 128; ++i)
{
v[i] = -3000.0 + 6000.0 * (i) / 127.0;
}
Solution
QSlider
only operates with int
steps, so you'd just need to do the calculation yourself:
#include <QtGui>
#include <cmath>
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget() : QWidget() {
slider_ = new QSlider;
slider_->setRange(0, 127);
connect(slider_, SIGNAL(valueChanged(int)), SLOT(ChangeSpinBox(int)));
box_ = new QDoubleSpinBox;
box_->setRange(-3000.0, 3000.0);
connect(box_, SIGNAL(valueChanged(double)), SLOT(ChangeSlider(double)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(slider_);
layout->addWidget(box_);
setLayout(layout);
}
private slots:
void ChangeSpinBox(int sliderValue) {
if (convertSpinBoxValueToSlider(box_->value()) != sliderValue) {
box_->setValue((6000.0 * sliderValue / 127.0) - 3000.0);
}
}
void ChangeSlider(double spinBoxValue) {
slider_->setValue(convertSpinBoxValueToSlider(spinBoxValue));
}
private:
QSlider *slider_;
QDoubleSpinBox *box_;
static int convertSpinBoxValueToSlider(double value) {
return qRound((value + 3000.0) * 127.0 / 6000.0);
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}
#include "main.moc"
Or am I not understanding your question?
OTHER TIPS
First of all, you have a bug in your vector computation. If you want to divide a range to X parts, then your vector must be of size X+1 not X. Example: you want to split range 0-10 to 5 parts. How many items you need? Your code suggests 5. But the answer is [0,2,4,6,8,10] = 6. So have vector 129 and do the division by 128.
So you would like to have a step size of 6000/128 = 48.875 exactly. You can do that with QDoubleSpinBox
QDoubleSpinBox::setRange(-3000.0,3000.0);
QDoubleSpinBox::setSingleStep(48.875);
but not with QSlider
which takes only integer. You can avoid the rounding error by multiplying your range by 1000.
QSlider::setRange(-3000000,3000000);
QSlider::setSingleStep(48875);
For setting the selected result to the spinbox, you need to divide by 1000 again of course.