I have some data available in fixed point 16.16 that I'd like to modify with a QDoubleSpinbox.
The problem is that whenever I convert my fixed point to a double there is the inevitable rounding issue.
double = fixed / (double)65536
Take for example 0.2
, in fixed point this is 0x00003333
, but when I put it into the QDoubleSpinBox it shows 0.19999694824188
.
But as soon as I touch the spinbox, it rounds to 0.2
.
How to make the QDoubleSpinBox round upon setValue?
The QDoubleSpinbox is behaving as designed, but my QAbstractTableModel
was not.
When the controls of the spinbox are not visible, the table implicitly uses a QString to display the value given in the QVariant.
After implementing the paint()
function with a fixed precision setnum()
in the qstring, it works great!
A quick test shows that this works here:
double valueToSet = 0x00003333 / (double)65536;
ui.doubleSpinBox->setValue(valueToSet);
The UI shows 0.20. The spinbox does round when calling setValue()
. You can check the source code yourself. I suggest stepping into the function with a debugger to see what is different for you.