I have seen that there is a
QDoubleValidator::ScientificNotation
is it possible to set the validator of qdoublespinbox to such a Validator?
How would that work?
I have seen that there is a
QDoubleValidator::ScientificNotation
is it possible to set the validator of qdoublespinbox to such a Validator?
How would that work?
Perhaps you could construct your own QDoubleValidator and set it via QAbstractSpinBox::lineEdit() [protected].
J-P Nurmi
Yes, but in the end it means to reimplement qdoublespinbox from scratch. I also looked at the possibility to derive from qdoublespinbox, but that is not possible especially because the setvalue is not virtual (rounding is then hardcoded). I described that problem in detail on the qt-interest mailinglist. This in the end still seems like I have to reinvent the wheel.
From scratch? All I meant was to change the validator of the QLineEdit that is inside the spin box.
Did you read the detailed description? It clearly says that:I also looked at the possibility to derive from qdoublespinbox, but that is not possible especially because the setvalue is not virtual (rounding is then hardcoded). I described that problem in detail on the qt-interest mailinglist. This in the end still seems like I have to reinvent the wheel.
The spin box supports double values but can be extended to use different strings with validate(), textFromValue() and valueFromText().
J-P Nurmi
Ok, I misread that. However it still wont work, see below
I know that these are virtual. But the round function inside setValue (both not virtual) are not changeable.
It is used in setValue:Qt Code:
double QDoubleSpinBoxPrivate ::round(double value) const { return q->locale().toDouble(strDbl); }To copy to clipboard, switch view to plain text mode
this unfortunately would round up the digits, even if the value would be 1e-20, which would become 0 at decimals = 4. Unaccaptable for my intention. I instead want 1.02349e-20 to become 1.0235e-20 at the round function.Qt Code:
{ d->setValue(v, EmitIfChanged); }To copy to clipboard, switch view to plain text mode
So that is where is my problem. If this could be solved, and I can set the Validator as you suggested, I could do it without reimplementing everything.
Have you tried changing QDoubleSpinBox::decimals?
J-P Nurmi
Changing decimals is not a solution. As long as the rounding is using 'f' (see http://doc.trolltech.com/4.4/qstring.html#arg-20) for QString, which means that exponentials are mapped to non exponentialst the decimal value becomes useless anyway.
Maybe you can avoid rounded() being called at all?
That becomes a principle question on C++ inheritance: If I have a function in the new class with the same Name and Properties as in the base class, will that work?
Also since round is not virtual, it means that I have to reimplement every function that uses round() and every function that calls a function using round etc.
That would be at least theses
Qt Code:
double QDoubleSpinBoxPrivate::round(double value) const { return q->locale().toDouble(strDbl); } { d->setValue(v, EmitIfChanged); } { d->decimals = qMax(0, decimals); setRange(minimum(), maximum()); // make sure values are rounded setValue(value()); } { d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m)); } { d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m); } { }To copy to clipboard, switch view to plain text mode
So in the end it may work, but is a lot more than I initially wanted to change...
Or am I thinking the wrong way?
I tried to implement setValue in the derived class, but that brings me to the next problem:
The original Code is this
Qt Code:
{ d->setValue(v, EmitIfChanged); }To copy to clipboard, switch view to plain text mode
I changed it to
Qt Code:
{ Q_OBJECT public: virtual ~QScienceSpinBox(); double round(double value) const; void setValue(double value); ... }; void QScienceSpinBox::setValue(double value) { QAbstractSpinBoxPrivate::setValue(v, EmitIfChanged); }To copy to clipboard, switch view to plain text mode
the setValue calls a setValue from deep inside the derived class. However I cannot call QAbstractSpinBoxPrivate, since it is not known within "<QtGui/QDoubleSpinBox>". All the other functions also call their equivalents from deep inside the base class, which all work with QVariants.
If I start replacing all this calls, no data ever gets inside the members of QDoubleSpinBox and I actually rewrite QDoubleSpinBox.
So how should I proceed?
I think this is a wrong approach. Instead of fighting with the decimals, you should aim at converting the visual output of the spinbox to scientific notation and vice versa. Maybe you can leave the internal representation as it is now.
I have also thougth of that, but it is not possible. If I set the internal decimals to lets say 1000, then at the round function it will nevertheless be rounded to the maximum of decimals of a double. So at 1e-34 which is very likely more digits than a double without exponential can represent if will be rounded to 0. (Not tested, just my interpretation of the code)
Matthias
Even if you use the scientific notation internally this is represented as a double so the precision will be limited nevertheless. So here it doesn't matter.
I have now a working solution. However the code amount is about 600 LOC.
This widget is derived from QDoubleSpinBox. It uses a decimal value of 1000 (that is more decimal points than a double can handle) and implements a new decimal value for the presentation in scientific notation. The Validator is realised by setting the LineEdit to a QDoubleValidator::ScientificNotation. However the most important part is the reimplementation of textFromValue and valueFromText. This unfortunately requires to copy the whole validation code of QDoubleSpinBox, which can not be borrowed and represents the major part of the code.
Since I can not paste the whole code I put it online at:
http://www.matthiaspospiech.de/blog/...ific-notation/
Can this code be shrinked by keeping the functionality?
Bookmarks