Allow SpinBox components to contain fractional values

This commit is contained in:
casper 2022-02-08 15:52:49 +01:00
parent a0e5d66299
commit 9c2d370e72
2 changed files with 58 additions and 28 deletions

View file

@ -4,44 +4,74 @@
import QtQuick 2.2
import QtQuick.Controls 2.15
SpinBox
Item
{
id: base
height: spinBox.height
property string prefix: ""
property string suffix: ""
property int decimals: 0
property real stepSize: 1
property real value: 0
property real from: 0
property real to: 99
signal editingFinished()
property alias wrap: spinBox.wrap
valueFromText: function(text)
{
return parseFloat(text.substring(prefix.length, text.length - suffix.length), decimals);
}
property bool editable: true
textFromValue: function(value)
{
return prefix + value.toFixed(decimals) + suffix
}
validator: RegExpValidator
property var validator: RegExpValidator
{
regExp: new RegExp("^" + prefix + "([0-9]+[.|,]?[0-9]*)?" + suffix + "$")
}
contentItem: TextField
signal editingFinished()
SpinBox
{
text: base.textFromValue(base.value, base.locale)
selectByMouse: true
background: Item {}
id: spinBox
anchors.fill: base
stepSize: 1
value: Math.floor(base.value / base.stepSize)
from: Math.floor(base.from / base.stepSize)
to: Math.floor(base.to / base.stepSize)
editable: base.editable
valueFromText: function(text)
{
return parseFloat(text.substring(prefix.length, text.length - suffix.length)) / base.stepSize;
}
textFromValue: function(value)
{
return prefix + (value * base.stepSize).toFixed(decimals) + suffix;
}
validator: base.validator
onActiveFocusChanged:
onValueModified:
{
if(!activeFocus)
base.value = value * base.stepSize;
}
contentItem: TextField
{
text: spinBox.textFromValue(spinBox.value, spinBox.locale)
selectByMouse: base.editable
background: Item {}
validator: base.validator
onActiveFocusChanged:
{
base.editingFinished()
if(!activeFocus)
{
base.editingFinished();
}
}
}
}
}
}