Results 1 to 6 of 6

Thread: Custom range for a Spinbox?

  1. #1
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Custom range for a Spinbox?

    Is it possible to create a custom range for a spinbox? Additionally, that range could be changed at run time. The range would have a initial value (design time) of -20 to 25. Those values are for a spinner that determines zoom ratio for a loaded image. The reason for the custom range is that I cannot have zero in the range of values - either the initial range or any calculated range thereafter. If re-implementing the class, how then would you keep the spinner from selecting zero in the range from minimum to maximum? It would have to, for instance go from -1 to 1 or vice-versa.

    If I created a custom spinner class and in designer promoted the spinner to use it, using the validate method doesn't help in "skipping" zero when clicking up/down within the range, unless I am missing something.

    Thanks in advance.

  2. #2
    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: Custom range for a Spinbox?

    You can either reimplement fixup() or reimplement textFromValue() and valueFromText().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom range for a Spinbox?

    Quote Originally Posted by wysota View Post
    You can either reimplement fixup() or reimplement textFromValue() and valueFromText().
    Thanks obi wan wysota.

    I looked at the icons example for a possible solution to refer to. This shows the use of re-implemented methods (the last two of the three mentioned) except fixup();

    I put together a re-implemented fixup as follows:
    Qt Code:
    1. void ScaleSpinBox::fixup(QString &str) const
    2. {
    3. bool ok;
    4. int value = str.toInt(&ok, 10);
    5.  
    6. if(ok)
    7. {
    8. if (value == 0)
    9. {
    10. value = -1;
    11. str = QString::number(value);
    12. }
    13. }
    14.  
    15. else
    16. {
    17. QSpinBox::fixup(str);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    The logic is just a quick stab at getting something started. However, it never gets called.
    Last edited by astodolski; 21st August 2013 at 17:04.

  4. #4
    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: Custom range for a Spinbox?

    fixup is called when the validator states the input is not valid. Thus you probably have to reimplement validate() as well. You can even fix the value there as well. However remember that in your case "0" is a perfectly good intermediate value.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Custom range for a Spinbox?

    Quote Originally Posted by wysota View Post
    fixup is called when the validator states the input is not valid. Thus you probably have to reimplement validate() as well. You can even fix the value there as well. However remember that in your case "0" is a perfectly good intermediate value.
    Yes I see that zero being valid always returned Acceptable . I guess a test range that never sees a zero and then go outside that range would force a call to QAbstractSpinBox::fixup(). In fixup() I then have to come up with handling the prior value to determine what the stepBy value would be. Perhpaps it be better to just handle the cached value of the last value of the spinbox and then call directly stepBy.

  6. #6
    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: Custom range for a Spinbox?

    It won't work if someone just types "0" instead of using arrows.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    astodolski (22nd August 2013)

Similar Threads

  1. Retaining a spinbox value
    By positive_4_life in forum Newbie
    Replies: 2
    Last Post: 19th November 2009, 17:36
  2. Getting the value from a spinbox
    By X-man in forum Newbie
    Replies: 14
    Last Post: 28th September 2007, 01:27
  3. spinbox
    By mickey in forum Newbie
    Replies: 5
    Last Post: 26th July 2006, 19:09
  4. spinbox
    By mickey in forum Newbie
    Replies: 2
    Last Post: 22nd May 2006, 08:35
  5. Custom SpinBox
    By Luis Rodriguez in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2006, 18:25

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.