Reputation: 416
I am building an application that makes use of multiple frequency ranges. I need the user to be able to increase/decrease the values in the QDoubleSpinBox
, as well as type in the value. If I increase the value to a number out of one range, I would like the value to jump to the next range (same principle for decreasing a value).
Does a QDoubleSpinBox
provide this kind of behavior? I can't seem to find an answer for what I'm looking for. I've tried setting a range using QDoubleValidator
, but I don't think it supports multiple ranges (unless I'm missing something). I've also tried using range checks with if statements with the valueChanged()
signal that gets emitted, but there must be a simpler way, right?
Here's an example of how I'd like the doubleSpinBox to behave:
Starting Value: 9.75
Range 1: 9.75 - 9.95
Range 2: 10.15 - 10.40
Range 3: 17.2 - 20.4
If value goes above 9.95, jump to 10.15.
If value goes above 10.40, jump to 17.2, etc.
I would also like to have the same behavior when decreasing the value (jumping back down to Range 1 if value drops below 10.15).
I would like to do this without writing multiple if/else if
statements if I can avoid it.
Upvotes: 1
Views: 532
Reputation: 6584
Trying to create multiple ranges is not a good solution. You should consider your problem in a different way: you have a single range from 9.75 to 20.40 with forbidden values.
So, if you override method such as QDoubleSpinBox::stepsBy()
and QDoubleSpinBox::valueFromText()
, you will be able to discard the values outside your ranges:
class Spinbox: public QDoubleSpinBox
{
public:
Spinbox(): QDoubleSpinBox()
{
setRange(9.75, 20.4);
setSingleStep(0.1);
}
virtual void stepBy(int steps) override
{
double const newValue = checkValue(value() + (steps * singleStep()));
setValue(newValue);
}
virtual double valueFromText(QString const& text) const override
{
qDebug() << text;
double const newValue = QDoubleSpinBox::valueFromText(text);
return checkValue(newValue);
}
double checkValue(double newValue) const
{
qDebug() << newValue << value();
if (9.95 < newValue && newValue < 10.15)
{
if (newValue > value())
return 10.15;
else
return 9.95;
}
else if (10.4 < newValue && newValue < 17.2)
{
if (newValue > value())
return 17.2;
else
return 10.4;
}
return newValue;
}
};
Upvotes: 1