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. Trouble with selectedText()
QtWS25 Last Chance

Trouble with selectedText()

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 617 Views
  • 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.
  • C Offline
    C Offline
    Circuits
    wrote on last edited by
    #1

    I am building a basic numeric number pad with Repeater:

    Repeater
                {
                  id: repeaterID
                  model: [7,8,9,4,5,6,1,2,3]
                  delegate: PushButton
                  {
                    height: 75
                    width: height
                    text: modelData
                    onClicked:
                    {
                      if(entryDisplay.selectedText.length > 0)
                      {
                        console.info("first")
                        console.info(entryDisplay.selectedText.length)
                        entryDisplay.text = modelData
                      }
                      else
                      {
                        console.info("second")
                        console.info(entryDisplay.selectedText.length)
                        entryDisplay.text += modelData
                      }
                    }
                  }
                }
    

    and as you can see I am attempting to create logic s/t if the string in the TextInput:

              TextInput
              {
                id: entryDisplay
                width: parent.width - 50
                anchors.centerIn: display
                color: "black"
                text: Math.round(settingValue.value).toString()
                onActiveFocusChanged:
                {
                  if(activeFocus)
                  {
                    selectAll();
                  }
                }
              }
    

    is currently selected then if another button is pressed it will erase that string and replace it with the value of the button pressed. The string is selected when the component is created and when the user clicks somewhere in the actual text field. It's working but only on the first go around. Here is the output of the first three button presses:

    qml: first
    qml: 1
    qml: second
    qml: 0
    qml: second
    qml: 0
    

    now if I then click inside the text field the text gets selected like it is supposed to.. but... here is the output of the next button press:

    qml: second
    qml: 0
    

    in other words, it is not recognizing the selected text. The output should have been:

    qml: first
    qml: 1
    

    Any idea what I am doing wrong here?

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

      Hi @Circuits , what is happening is that even if you click on the PushButton the TextInput is still in focus.

      Just try to make this code change:-

      onClicked:
                      {
                          console.log(entryDisplay.selectedText.length)
                          if(entryDisplay.selectedText.length > 0)
                          {
                              console.info("first")
                              console.info(entryDisplay.selectedText.length)
                              entryDisplay.text = modelData
                          }
                          else
                          {
                              console.info("second")
                              console.info(entryDisplay.selectedText.length)
                              entryDisplay.text += modelData
                          }
       //Just add the below line in your code (rootButton is PushButton)
                          rootButton.forceActiveFocus()
                      }
      

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

      1 Reply Last reply
      2
      • C Offline
        C Offline
        Circuits
        wrote on last edited by Circuits
        #3

        Unfortunately I still can't get it to work. I tried giving the PushButton an id called "theButton":

                    Repeater
                    {
                      id: repeaterID
                      model: [7,8,9,4,5,6,1,2,3]
                      delegate: PushButton
                      {
                        id: theButton
                        height: 75
                        width: height
                        text: modelData
                        onClicked:
                        {
                          console.log(entryDisplay.selectedText.length)
                          if(entryDisplay.selectedText.length > 0)
                          {
                            console.info("first")
                            console.info(entryDisplay.selectedText.length)
                            entryDisplay.text = modelData
                          }
                          else
                          {
                            console.info("second")
                            console.info(entryDisplay.selectedText.length)
                            entryDisplay.text += modelData
                          }
                          theButton.forceActiveFocus()
                        }
                      }
                    }
        

        output first 3 button presses:

        qml: 1
        qml: first
        qml: 1
        qml: 0
        qml: second
        qml: 0
        qml: 0
        qml: second
        qml: 0
        

        clicked inside of the text area to select the text and clicked another button:

        qml: 0
        qml: second
        qml: 0
        

        I think I agree that this is a focus problem. Is there some way for me to print what has focus?

        1 Reply Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #4

          @Circuits said in Trouble with selectedText():

          I think I agree that this is a focus problem. Is there some way for me to print what has focus?

          Use activeFocusItem property change signal of root Window. It is also useful to set names to objects via objectName property.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Circuits
            wrote on last edited by
            #5

            I would but I am not using Window.

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

              Hi @Circuits , i just copy pasted your code its working fine for me.

              7004f38e-cde9-417f-98b1-f031ad954378-image.png

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

              C 1 Reply Last reply
              1
              • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                Hi @Circuits , i just copy pasted your code its working fine for me.

                7004f38e-cde9-417f-98b1-f031ad954378-image.png

                C Offline
                C Offline
                Circuits
                wrote on last edited by Circuits
                #7

                @Shrinidhi-Upadhyaya This code?:

                Repeater
                {
                  id: repeaterID
                  model: [7,8,9,4,5,6,1,2,3]
                  delegate: PushButton
                  {
                    id: theButton
                    height: 75
                    width: height
                    text: modelData
                    onClicked:
                    {
                      console.info(entryDisplay.selectedText.length)
                      if(entryDisplay.selectedText.length > 0)
                      {
                        console.info("first")
                        console.info(entryDisplay.selectedText.length)
                        entryDisplay.text = modelData
                      }
                      else
                      {
                        console.info("second")
                        console.info(entryDisplay.selectedText.length)
                        entryDisplay.text += modelData
                      }
                      theButton.forceActiveFocus()
                    }
                  }
                }
                

                I really can't understand why it's not working for me. I wish I was better with the QML debugger. Perhaps I will try re-writing the whole component. There must be a deeper problem.

                1 Reply Last reply
                0

                • Login

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