Results 1 to 14 of 14

Thread: qdoublespinbox with scientific notation

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,359
    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,359
    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.

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.