Results 1 to 10 of 10

Thread: QDoubleSpinBox and Float Values

  1. #1
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default QDoubleSpinBox and Float Values

    I tried to use QDoubleSpinBox to accept values with 1 decimal place.

    I use the "setFloatDomain(true)" and "setDecimals(1)". For getting a result of "6.4"

    1) when i type "6" it shows 6.0.

    2) Then if i type ".4", it is not emitting the valueChanged() signal. Hence the spinbox only showing "6.0"

    Pl'se recommend suitable suggestions..and what all i have to do to make the spinbox works with the below condition.

    1) Spinbox should accept and shows values with 1 decimal place.

  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: QDoubleSpinBox and Float Values

    What is "setFloatDomain()"? Which version of Qt are you using? I can't reproduce your misbehaviour.

  3. #3
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: QDoubleSpinBox and Float Values

    I am using QT4.3.2, and i have subclassed QDoubleSpinBox,

    Initially QDoubleSpinBox shows 0.0

    If i highlight that and try entering "6.4". the following will results

    While entering "6". Its emitting valueChanged() signal, and i format that value in the "textFromValue()" to make the QDoubleSpinBox to show "6.0"

    Then upon entering "." and "4" it is not at all emitting valueChanged() signal,

    Next time if i Highlight "0" in "6.0" and enter 4, its emitting valueChanged() signal. How could i rectify this problem.

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDoubleSpinBox and Float Values

    I guess that's because there is already a decimal point and therefore when you try to enter another decimal point QDoubleSpinBox is not emitting any signal.

  5. #5
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: QDoubleSpinBox and Float Values

    But then what about when i type "4" after the "."

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDoubleSpinBox and Float Values

    But I am not able to reproduce it. Signals are getting emitted correctly.

  7. #7
    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 and Float Values

    Why have you reimplemented textFromValue() in such a manner? Doesn't the spinbox already show the value as 6.0 when you finish editing?

  8. #8
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: QDoubleSpinBox and Float Values

    I have found out the problem

    1) I have used some thing like

    connect(doubleSpinBox, SIGNAL(valueChanged(double)),
    this, SLOT(changeValueSlot(double)));


    2) Then I tried to use doubleSpinBox->setValue(value) with in the changeValueSlot(double value).

    This creates the problem. Thanks for all the suggestions.

  9. #9
    Join Date
    Jun 2008
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post

    Default Re: QDoubleSpinBox and Float Values

    Is it possible to type letters in QDoubleSpinBox. I have to type the letter "e" into QDoubleSpinbox.

    I tried to do that in the spinboxes and icons example provided in the "4.3.2\examples\widgets\", but failed.

    How could i make it work.

  10. #10
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDoubleSpinBox and Float Values

    You can enter any character into the spinbox, the key method here is 'validate'. You need to override this and do the checking on what character has been entered. As an example, I have just recently done the following python code (still a work in progress ) for a project;

    Qt Code:
    1. class PowerSpinBox(QDoubleSpinBox):
    2. """
    3. Ensures values entered by the user are a multiple of 0.25 and within
    4. the range -/+20.00. The displayed text is also signed -/+, and can
    5. be entered with or without -/+.Unsigned user input is assumed to be
    6. positive.
    7. """
    8. def __init__(self, min=-20.00, max=20.00, step=0.25, decimals=2, parent=None):
    9. super(PowerSpinBox, self).__init__(parent)
    10. self.setRange(min,max)
    11. self.setSingleStep(step)
    12. self.setDecimals(decimals)
    13.  
    14. def textFromValue(self, value):
    15. #TODO: Should zero be unsigned?
    16. if value >= 0:
    17. return "+%.2f" % value
    18. else:
    19. return "%.2f" % value
    20.  
    21. def fixup(self, input):
    22. value = input.toDouble()[0]
    23. if (value % 0.25) > 0:
    24. if value < 0:
    25. value=value*-1
    26. minus=True
    27. else:
    28. minus=False
    29. newValue = float((int(value*100)/25)*25)/100
    30. if minus: newValue=newValue*-1
    31. newInput = "%.2f" % newValue
    32. if not minus: newInput = "+"+newInput
    33. print"Invalid value [%s] replaced with [%s]" % (input, newInput)
    34. QApplication.beep()
    35. input.replace(0, input.length(), newInput)
    36.  
    37. def validate(self, input, pos ):
    38. valid = QValidator.Invalid
    39. newChar = input.at(pos-1)
    40. if pos < 1:
    41. valid = QValidator.Acceptable
    42. else:
    43. if not newChar.isDigit():
    44. if ((newChar.cell() == '.') and (input.count('.') == 1)) or \
    45. ((newChar.cell() == '-') and (pos == 1)) or \
    46. ((newChar.cell() == '+') and (pos == 1)):
    47. valid = QValidator.Acceptable
    48. else:
    49. valid = QValidator.Invalid
    50. else:
    51. value = input.toDouble()[0]
    52. if (not (value >= self.minimum() and value <= self.maximum())) or \
    53. (input.length() > input.indexOf('.')+self.decimals()+1):
    54. valid = QValidator.Invalid
    55. elif (value % 0.25) > 0:
    56. valid = QValidator.Intermediate
    57. else:
    58. valid = QValidator.Acceptable
    59. return (valid, pos)
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to fifth for this useful post:

    George Neil (7th August 2008)

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.