Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Disable TextInput update during editing
Forum Updated to NodeBB v4.3 + New Features

Disable TextInput update during editing

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 800 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    My TextInput is bound to a C++ function, like this:

    TextInput {
        text: MyApp.foo()
    }
    

    of course, I need to disable updates when editing. I tried something like this:

    Item {
        property bool _dirty: false
        property bool _cancel: false
        property string _oldText
    
        TextInput {
            color: _dirty ? "red" : "forestgreen"
            text: {
                var value = MyApp.foo()
                if (_dirty) {
                    if (parseFloat(value) === parseFloat(text)) _dirty = false
                    return text
    
                } else return value === undefined ? "-" : parseFloat(value).toFixed(2)
            }
    
            Keys.onEscapePressed: {
                _cancel = true
                focus = false
            }
    
            onEditingFinished: {
                if (_cancel) {
                    text = _oldText
                    _oldText = ""
                    _dirty = false
                    _cancel = false
    
                } else {
                    MyApp.write(text)
                    focus = false
                }
            }
    
            onActiveFocusChanged: {
                if (activeFocus) {
                    _oldText = text
                    selectAll()
                    _dirty = true
                }
            }
        }
    }
    

    The expected behavior is the following:

    1. normally the text is given by the foo() value, green color
    2. when the user enter in the fields, the color turns red and you can edit the text
    3. if you press 'esc' it ends restoring the previous value and set the green color
    4. if you press 'enter' it calls a C++ functions and waits until the foo() value is equal to the inserted one. When this happens it turns green and binds again with the foo value

    The problem is it complains about a binding loop for property "text".
    It doesn't say the exact line but I might guess is:

    return text
    

    I thought to keep the current value it would be enough to return the actual one. Is it wrong?

    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      Hi @Mark81 , here is the sample code:-

      Rectangle {
              id: root
      
              height: 100
              width: 100
              color: "transparent"
              border.color: "red"
      
              property string oldTxt: 'Hello World'
      
              TextInput {
                  id: inpTxt
      
                  color: activeFocus ? "red" : "forestgreen"
                  anchors.fill: parent
      
                  Keys.onEscapePressed: {
                      focus = false
                      root.foo('')
                  }
      
                  Keys.onReturnPressed: {
                      root.oldTxt = inpTxt.text
                      focus = false
                      root.foo(text)
                  }
              }
      
              function foo(newText) {
                  if(newText === '')
                      inpTxt.text = root.oldTxt
                  else
                      inpTxt.text = newText
              }
      
              Component.onCompleted: {
                  root.foo('')
              }
          }
      

      Note:- as its just a sample code, i have called the javascript function, you need to call the c++ function as you are doing it now.

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      M 1 Reply Last reply
      2
      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

        Hi @Mark81 , here is the sample code:-

        Rectangle {
                id: root
        
                height: 100
                width: 100
                color: "transparent"
                border.color: "red"
        
                property string oldTxt: 'Hello World'
        
                TextInput {
                    id: inpTxt
        
                    color: activeFocus ? "red" : "forestgreen"
                    anchors.fill: parent
        
                    Keys.onEscapePressed: {
                        focus = false
                        root.foo('')
                    }
        
                    Keys.onReturnPressed: {
                        root.oldTxt = inpTxt.text
                        focus = false
                        root.foo(text)
                    }
                }
        
                function foo(newText) {
                    if(newText === '')
                        inpTxt.text = root.oldTxt
                    else
                        inpTxt.text = newText
                }
        
                Component.onCompleted: {
                    root.foo('')
                }
            }
        

        Note:- as its just a sample code, i have called the javascript function, you need to call the c++ function as you are doing it now.

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @Shrinidhi-Upadhyaya thanks for your hints! Out of curiosity, what should be the return value of a property to maintain the current value?

        1 Reply Last reply
        0
        • Shrinidhi UpadhyayaS Offline
          Shrinidhi UpadhyayaS Offline
          Shrinidhi Upadhyaya
          wrote on last edited by
          #4

          @Mark81 , you mean the current text right?It should be a string :-)

          Shrinidhi Upadhyaya.
          Upvote the answer(s) that helped you to solve the issue.

          M 1 Reply Last reply
          0
          • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

            @Mark81 , you mean the current text right?It should be a string :-)

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @Shrinidhi-Upadhyaya said in Disable TextInput update during editing:

            @Mark81 , you mean the current text right?It should be a string :-)

            I mean, if you have a Text object you can set its text property to any value in two ways:

            text: myTextVar
            

            or

            text: { return myTextVar }
            

            the second way allows you do to more complex things, i.e.:

            text: {
                if (myFooVar === 1) return "blabla"
                else if (myFoo2Var === 10) return myTextVar1
                else return myTextVar2
            }
            

            but sometime you might need to leave the value unchanged, I mean keeping the last value. I'm aware I can do it using another variable but it become clumsy:

            text: {
                var currText
                if (myFooVar === 1) currText = "blabla"
                else if (myFoo2Var === 10) currText = myTextVar1
                else currText = oldText // keep the current content
            
                oldText = currText
                return currText
            }
            

            I though it was fine and more readable to return the actual value:

            text: return text
            

            but it creates a binding-loop error (I hoped this case was handled as an exception in order to keep the current value).
            So I wonder if Qt developer provided a way to keep the current value or we need to use another variable like the example above.

            1 Reply Last reply
            0
            • Shrinidhi UpadhyayaS Offline
              Shrinidhi UpadhyayaS Offline
              Shrinidhi Upadhyaya
              wrote on last edited by
              #6

              Hi @Mark81 ,yeah it depends upon how complex things you need to handle, for example if you just need to handle only 2 scenarios like if the value of "myFooVar" is "1", you want to display "Hello" else "Bye" then, you can do like

               text: myFooVar === 1 ? "Hello" : "Bye"
              

              I dont think there is a predefined property or a function to retrieve or store the last value.

              Shrinidhi Upadhyaya.
              Upvote the answer(s) that helped you to solve the issue.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved