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. Remember the last focused TextEdit and insert text inside it on clicking a button.
Qt 6.11 is out! See what's new in the release blog

Remember the last focused TextEdit and insert text inside it on clicking a button.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 447 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.
  • F Offline
    F Offline
    foobarbaz
    wrote on last edited by
    #1

    Dear Qt

    Suppose I have several TextInput fields and a button. Clicking the button should insert some text (let say "foo") inside the text field that had the focus right before clicking the button. I was thinking of the two following approaches:

    Approach #1

    1. We have a property var lastFocusedText which is intended to store the last focused text field
    2. Once the user changes the focus of a text field the lastFocusedText is updated
    3. On clicking we check if lastFocusedText is valid and if so do text = "boo"

    This is a sketch of what I mean :

    Item {
      id: root
    
      TextInput {
        id: textA
    
        onActiveFocusChanged : {
          if(activeFocus === true) {
            root.lastFocusedText = textA
          }
          else {
            root.lastFocusedText = null
          }
        }
      }
    
      TextInput {
        id: textB
    
        onActiveFocusChanged : {
          if(activeFocus === true) {
            root.lastFocusedText = textB
          }
          else {
            root.lastFocusedText = null
          }
        }
      }
    
      Button {
        focusPolicy: Qt.NoFocus
        onClicked : {
          if(root.lastFocusedText instanseof TextInput) {
            root.lastFocusedText.text = "Foo"
          }
        }
      }
    }
    

    Approach #2

    This one is more simple :

    Every text field is subscribed to a signal and if it has active focus changes its text:

    Item {
      id: root
    
      TextInput {
        id: textA
    
        Connections {
          target: booButton
          function onClicked() {
            if(textA.activeFocus === true) {
              textA.text = "Foo"
            }
          }
        }
      }
    
      TextInput {
        id: textB
    
        Connections {
          target: booButton
          function onClicked() {
            if(textB.activeFocus === true) {
              textB.text = "Foo"
            }
          }
        }
      }
    
      Button {
        id: booButton
        focusPolicy: Qt.NoFocus
      }
    }
    

    I like second approach more. But what do you think ?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      Another alternative would be to use the Window.activeFocusItem attached property. On button click, check if it's one one your TextInput ( item.parent === root && item.instanceof TextInput) and set its text.

      Reading the code I prefer to have an onClicked in the Button, to keep the cause and effects close together.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        foobarbaz
        wrote on last edited by
        #3

        @GrecKo Thank you ! Indeed I can use that property :)

        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