PDA

View Full Version : QDoubleSpinBox and Float Values



George Neil
7th July 2008, 14:46
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.

wysota
7th July 2008, 15:04
What is "setFloatDomain()"? Which version of Qt are you using? I can't reproduce your misbehaviour.

George Neil
8th July 2008, 10:08
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.

munna
8th July 2008, 10:50
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.

George Neil
8th July 2008, 10:55
But then what about when i type "4" after the "."

munna
8th July 2008, 11:01
But I am not able to reproduce it. Signals are getting emitted correctly.

wysota
8th July 2008, 15:55
Why have you reimplemented textFromValue() in such a manner? Doesn't the spinbox already show the value as 6.0 when you finish editing?

George Neil
9th July 2008, 09:25
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. :)

George Neil
5th August 2008, 09:06
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. :(

fifth
5th August 2008, 21:22
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;


class PowerSpinBox(QDoubleSpinBox):
"""
Ensures values entered by the user are a multiple of 0.25 and within
the range -/+20.00. The displayed text is also signed -/+, and can
be entered with or without -/+.Unsigned user input is assumed to be
positive.
"""
def __init__(self, min=-20.00, max=20.00, step=0.25, decimals=2, parent=None):
super(PowerSpinBox, self).__init__(parent)
self.setRange(min,max)
self.setSingleStep(step)
self.setDecimals(decimals)

def textFromValue(self, value):
#TODO: Should zero be unsigned?
if value >= 0:
return "+%.2f" % value
else:
return "%.2f" % value

def fixup(self, input):
value = input.toDouble()[0]
if (value % 0.25) > 0:
if value < 0:
value=value*-1
minus=True
else:
minus=False
newValue = float((int(value*100)/25)*25)/100
if minus: newValue=newValue*-1
newInput = "%.2f" % newValue
if not minus: newInput = "+"+newInput
print"Invalid value [%s] replaced with [%s]" % (input, newInput)
QApplication.beep()
input.replace(0, input.length(), newInput)

def validate(self, input, pos ):
valid = QValidator.Invalid
newChar = input.at(pos-1)
if pos < 1:
valid = QValidator.Acceptable
else:
if not newChar.isDigit():
if ((newChar.cell() == '.') and (input.count('.') == 1)) or \
((newChar.cell() == '-') and (pos == 1)) or \
((newChar.cell() == '+') and (pos == 1)):
valid = QValidator.Acceptable
else:
valid = QValidator.Invalid
else:
value = input.toDouble()[0]
if (not (value >= self.minimum() and value <= self.maximum())) or \
(input.length() > input.indexOf('.')+self.decimals()+1):
valid = QValidator.Invalid
elif (value % 0.25) > 0:
valid = QValidator.Intermediate
else:
valid = QValidator.Acceptable
return (valid, pos)