Skip to content

Commit

Permalink
Fix race condition in ListTextField
Browse files Browse the repository at this point in the history
When editing a ListTextField, we would write the new value to the data point,
then emit 'root.accepted(text)'.
This causes problems for ListTextFields where the secondary text is different
to the raw value of the DataPoint, such as the generator service interval,
where the DataPoint stores the value in seconds, and the secondary text is
presented in hours, as follows (elided for brevity):

ListTextField {
    id: serviceInterval

    text: qsTrId("page_settings_generator_service_interval")
    secondaryText: Math.round(dataValue / 60 / 60)
    dataSource: settingsBindPrefix + "/ServiceInterval"
    onAccepted: function(hours) {
        setDataValue(hours * 60 * 60)
    }
}

The user might change the service interval to 1000 hours. The data point gets
set to '1000'. The 'serviceInterval' object detects that the 'dataValue' has
changed, and updates the secondary text to 1000 / 60 / 60, i.e. 0.2, which gets
displayed as 0. ListTextField continues its 'onAccepted' routine, and emits
'root.accepted(text)', which is now equal to 0. The 'serviceInterval' object
runs 'onAccepted', and calls 'setDataValue(hours * 60 * 60)', which in this
case, is equal to 0.
  • Loading branch information
DanielMcInnes committed Jul 17, 2023
1 parent cd79159 commit 4458496
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions components/listitems/ListTextField.qml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ ListItem {
text: dataValid ? dataValue : ""

onAccepted: {
let newValue = text
_accepted = true
if (dataPoint.source) {
dataPoint.setValue(text)
dataPoint.setValue(newValue)
}
textField.focus = false
root.accepted(text)
root.accepted(newValue)
}

onEditingFinished: root.editingFinished()
Expand Down
1 change: 1 addition & 0 deletions pages/settings/PageGeneratorRuntimeService.qml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Page {
textField.inputMethodHints: Qt.ImhDigitsOnly
dataSource: settingsBindPrefix + "/AccumulatedTotalOffset"
enabled: userHasWriteAccess && state.value === 0
visible: dataValid
textField.maximumLength: 6
onAccepted: function(hours) {
setDataValue(accumulatedTotalItem.value - hours * 60 * 60)
Expand Down

0 comments on commit 4458496

Please sign in to comment.