Results 1 to 14 of 14

Thread: qdoublespinbox with scientific notation

  1. #1
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qdoublespinbox with scientific notation

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qdoublespinbox with scientific notation

    Perhaps you could construct your own QDoubleValidator and set it via QAbstractSpinBox::lineEdit() [protected].
    J-P Nurmi

  3. #3
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    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.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qdoublespinbox with scientific notation

    Quote Originally Posted by pospiech View Post
    Yes, but in the end it means to reimplement qdoublespinbox from scratch.
    From scratch? All I meant was to change the validator of the QLineEdit that is inside the spin box.

    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.
    Did you read the detailed description? It clearly says that:
    The spin box supports double values but can be extended to use different strings with validate(), textFromValue() and valueFromText().
    J-P Nurmi

  5. #5
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    Quote Originally Posted by jpn View Post
    From scratch? All I meant was to change the validator of the QLineEdit that is inside the spin box.
    Ok, I misread that. However it still wont work, see below

    Quote Originally Posted by jpn View Post
    Did you read the detailed description? It clearly says that:
    I know that these are virtual. But the round function inside setValue (both not virtual) are not changeable.
    Qt Code:
    1. double QDoubleSpinBoxPrivate ::round(double value) const
    2. {
    3. Q_Q(const QDoubleSpinBox);
    4. const QString strDbl = q->locale().toString(value, 'f', decimals);
    5. return q->locale().toDouble(strDbl);
    6. }
    To copy to clipboard, switch view to plain text mode 
    It is used in setValue:
    Qt Code:
    1. void QDoubleSpinBox::setValue(double value)
    2. {
    3. QVariant v(d->round(value)); // round according to decimals
    4. d->setValue(v, EmitIfChanged);
    5. }
    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.

    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.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qdoublespinbox with scientific notation

    Quote Originally Posted by pospiech View Post
    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.
    Have you tried changing QDoubleSpinBox::decimals?
    J-P Nurmi

  7. #7
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    Quote Originally Posted by jpn View Post
    Have you tried changing QDoubleSpinBox::decimals?
    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qdoublespinbox with scientific notation

    Maybe you can avoid rounded() being called at all?

  9. #9
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    Quote Originally Posted by wysota View Post
    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:
    1. double QDoubleSpinBoxPrivate::round(double value) const
    2. {
    3. Q_Q(const QDoubleSpinBox);
    4. const QString strDbl = q->locale().toString(value, 'f', decimals);
    5. return q->locale().toDouble(strDbl);
    6. }
    7.  
    8. void QDoubleSpinBox::setValue(double value)
    9. {
    10. QVariant v(d->round(value));
    11. d->setValue(v, EmitIfChanged);
    12. }
    13.  
    14. void QDoubleSpinBox::setDecimals(int decimals)
    15. {
    16. d->decimals = qMax(0, decimals);
    17.  
    18. setRange(minimum(), maximum()); // make sure values are rounded
    19. setValue(value());
    20. }
    21.  
    22. void QDoubleSpinBox::setMinimum(double minimum)
    23. {
    24. const QVariant m(d->round(minimum));
    25. d->setRange(m, (d->variantCompare(d->maximum, m) > 0 ? d->maximum : m));
    26. }
    27.  
    28. void QDoubleSpinBox::setMaximum(double maximum)
    29. {
    30. const QVariant m(d->round(maximum));
    31. d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m);
    32. }
    33.  
    34. void QDoubleSpinBox::setRange(double minimum, double maximum)
    35. {
    36. d->setRange(QVariant(d->round(minimum)), QVariant(d->round(maximum)));
    37. }
    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?

  10. #10
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    I tried to implement setValue in the derived class, but that brings me to the next problem:

    The original Code is this
    Qt Code:
    1. void QDoubleSpinBox::setValue(double value)
    2. {
    3. QVariant v(d->round(value));
    4. d->setValue(v, EmitIfChanged);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I changed it to
    Qt Code:
    1. class QScienceSpinBox : public QDoubleSpinBox
    2. {
    3. Q_OBJECT
    4. public:
    5. QScienceSpinBox(QWidget* parent = 0);
    6. virtual ~QScienceSpinBox();
    7.  
    8. double round(double value) const;
    9.  
    10. void setValue(double value);
    11. ...
    12. };
    13.  
    14. void QScienceSpinBox::setValue(double value)
    15. {
    16. QVariant v(this->round(value));
    17. QAbstractSpinBoxPrivate::setValue(v, EmitIfChanged);
    18. }
    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?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qdoublespinbox with scientific notation

    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.

  12. #12
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    Quote Originally Posted by wysota View Post
    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

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qdoublespinbox with scientific notation

    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.

  14. #14
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qdoublespinbox with scientific notation

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.