Results 1 to 6 of 6

Thread: QDoubleSpinBox with nonlinear values

  1. #1
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QDoubleSpinBox with nonlinear values

    I would like to create spinBox which would have a concrete list of values with alternating interval beetwen positions on it.

    Something like that: 1.0, 5.0, 20.0, 35.0, 37.0, 39.0, 44.0, 50.0...

    Is there a simple way to do it?

  2. #2
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDoubleSpinBox with nonlinear values

    Hi !

    You must reimplement virtual void QAbstractSpinBox::stepBy(int steps)

    Here's a functionnal example :
    Qt Code:
    1. #ifndef MYSPINBOX_H
    2. #define MYSPINBOX_H
    3.  
    4. #include <QDoubleSpinBox>
    5.  
    6. class MySpinBox : public QDoubleSpinBox
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MySpinBox(QWidget *parent = 0) : QDoubleSpinBox(parent)
    11. {
    12. _values<<1.0<<5.0<<20.0<<35.0<<37.0<<39.0<<44.0<<50.0;
    13. setRange(_values.first(),_values.last());
    14. }
    15.  
    16. void stepBy(int steps)
    17. {
    18. int idx = findIdx(value());
    19. idx += steps>0 ? 1:-1;
    20. setValue(_values.value(idx));
    21. }
    22.  
    23. private:
    24. QList<double> _values;
    25.  
    26. int findIdx(double currVal)
    27. {
    28. for(int idx = 0 ; idx < _values.count()-1 ; ++idx)
    29. if(currVal>=_values.value(idx) && currVal<_values.value(idx+1)) return idx;
    30.  
    31. return _values.count()-1;
    32. }
    33. };
    34.  
    35. #endif // MYSPINBOX_H
    To copy to clipboard, switch view to plain text mode 

    ... my first answer on this forum ! ...

  3. The following user says thank you to thomas@itest for this useful post:

    code_err (21st February 2012)

  4. #3
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QDoubleSpinBox with nonlinear values

    Wow, it looks great. I can't write such an concise and elegant code so far. Thanks.

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QDoubleSpinBox with nonlinear values

    A couple of comments, if I may:

    1. Qt provides a lookup function for QList: QList::indexOf()
    2. But, the lookup is unnecessary if you make idx a member variable and increment it:
    Qt Code:
    1. class MySpinBox : public QDoubleSpinBox
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MySpinBox(QWidget *parent = 0) : QDoubleSpinBox(parent), _idx(0)
    6. {
    7. values<<1.0<<5.0<<20.0<<35.0<<37.0<<39.0<<44.0<<50.0;
    8. setRange(values.first(),values.last());
    9. }
    10.  
    11. void stepBy(int steps)
    12. {
    13. _idx += steps;
    14. setValue(_values.at(_idx));
    15. }
    16.  
    17. private:
    18. QList<double> _values;
    19. int _idx;
    20. };
    To copy to clipboard, switch view to plain text mode 

    Hope this helps!

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDoubleSpinBox with nonlinear values

    Range checking/clamping of idx might be a good idea. Crashes in debug mode if you Shift-PgUp for example. Doesn't crash in release mode but fails to operate correctly for me: e.g. press Shift-PgUp and then up arrow and you run through:
    Qt Code:
    1. //_idx _value.at(_idx)
    2. 10 6.71656e-317
    3. 11 3.95253e-323
    4. 12 6.71634e-317
    5. 13 4.44659e-323
    6. 14 3.21143e-322
    7. 15 6.95315e-310
    8. 16 4.88059e-313
    To copy to clipboard, switch view to plain text mode 
    where the index and data are rubbish. The up-arrow does not appear to function any longer and down remains unavailable.

  7. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QDoubleSpinBox with nonlinear values

    You're right Chris. I just "winged it" on this one as evidenced by the values/_values typo.

    Revised:
    Qt Code:
    1. class MySpinBox : public QDoubleSpinBox
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MySpinBox(QWidget *parent = 0) : QDoubleSpinBox(parent), _idx(0)
    6. {
    7. _values<<1.0<<5.0<<20.0<<35.0<<37.0<<39.0<<44.0<<50.0;
    8. setRange(_values.first(),_values.last());
    9. }
    10.  
    11. void stepBy(int steps)
    12. {
    13. _idx += steps;
    14. if(_idx<0) _idx=0;
    15. else if(_idx>=_values.count()) _idx=_values.count()-1;
    16. setValue(_values.at(_idx));
    17. }
    18.  
    19. private:
    20. QList<double> _values;
    21. int _idx;
    22. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 19th January 2011, 08:15
  2. QDoubleSpinBox
    By prashant in forum Qt Programming
    Replies: 0
    Last Post: 7th October 2009, 16:30
  3. QDoubleSpinBox and Float Values
    By George Neil in forum Qt Programming
    Replies: 9
    Last Post: 5th August 2008, 20:22
  4. QDoubleSpinBox
    By coderbob in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2008, 11:30
  5. Validator for QDoubleSpinBox
    By ehamberg in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2008, 11:37

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
  •  
Qt is a trademark of The Qt Company.