Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-125524

QQC2 SpinBox textFromValue not called when dependent values change

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.7
    • Quick: Controls 2
    • None
    • All

    Description

      The below is basically one of the customised SpinBox examples from the documentation, with the exception that the custom suffix value is bound to an editable TextField. The point of this is to demonstrate that if values which textFromValue reference change, then it is not automatically called to refresh the display. (This is presumably by design as if it was meant that the textFromValue property was to be re-evaluated automatically, it would be a QQmlExpression or some such instead of a QJSValue.) Anyway, the issue is less so that textFromValue isn't automatically called (I'm not even sure it's possible to fix that without a redesign?), but that when you know that changes have occurred that mean a call to textFromValue is necessary, there isn't an obvious or clean way to force an update.

      import QtQuick
      import QtQuick.Controls
      
      Window {
          width: 640
          height: 480
          visible: true
      
          Column {
              spacing: 10
              anchors.centerIn: parent
              
              TextField {
                  id: suffixTextInput
                  text: "m"
              }
      
              SpinBox {
                  id: spinBox
                  from: 0
                  value: 11
                  to: 100
                  editable: true
      
                  property string prefix: "L="
                  property string suffix: suffixTextInput.text
      
                  validator: RegularExpressionValidator { regularExpression: /\D*(-?\d*\.?\d*)\D*/ }
      
                  textFromValue: function(value, locale) {
                      return prefix + Number(value).toLocaleString(locale, 'f', 0) + suffix
                  }
      
                  valueFromText: function(text, locale) {
                      let re = /\D*(-?\d*\.?\d*)\D*/
                      return Number.fromLocaleString(locale, re.exec(text)[1])
                  }
      
                  function forceTextFromValueUpdateHack() {
                      let actualLocaleName = locale.name;
                      locale = Qt.locale("C");
                      locale = Qt.locale(actualLocaleName);
                  }
              }
      
              Button {
                  text: "Force Update"
                  onClicked: { spinBox.forceTextFromValueUpdateHack(); }
              }
          }
      } 

      You'll notice there is also a button which calls forceTextFromValueUpdateHack, which as its name implies, is a hack to force an update, capitalising on the fact that QQuickSpinBox::localeChange calls updateDisplayText. Ultimately what I'm asking for is a Q_INVOKABLE update/refresh function on QQuickSpinBox, which calls updateDisplayText, without having to resort to the above hack.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            timangus Tim Angus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes