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. FocusScope doesn't give the child focus properly

FocusScope doesn't give the child focus properly

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 2 Posters 1.7k Views 1 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi all,

    In the FocusScope example here using the following code, there's no focus shown when running the project. The tab key doesn't give/change the focus either!

    TLineEditV1.qml:

    import QtQuick 2.12
    
    FocusScope {
        width: 96; height: input.height + 8
        Rectangle {
            anchors.fill: parent
            color: "lightsteelblue"
            border.color: "gray"
        }
    
        property alias text: input.text
        property alias input: input
    
        TextInput {
            id: input
            width: 100; height: 20
            anchors.fill: parent
            anchors.margins: 4
            focus: true
        }
    }
    

    mani.qml:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.5
    
    Window {
        visible: true
        width: 600; height: 400
        color: "linen"
    
        TLineEditV1 {
            id: input1
            anchors.top: parent.top
            anchors.topMargin: 10
            text: "text 1"
        }
    
        TLineEditV1 {
            id: input2
            anchors.top: input1.bottom
            anchors.topMargin: 5
            text: "text 2"
        }
    }
    

    What is the problem, please?

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

      Hi @tomy , inside your FocusScope give this:-

      To have focus on tab give this:-

      activeFocusOnTab :true
      

      To default focus when you launch the project give this:-

      focus: true
      

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

      tomyT 1 Reply Last reply
      4
      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

        Hi @tomy , inside your FocusScope give this:-

        To have focus on tab give this:-

        activeFocusOnTab :true
        

        To default focus when you launch the project give this:-

        focus: true
        
        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by
        #3

        @Shrinidhi-Upadhyaya

        To default focus when you launch the project give this:-
        focus: true

        It's set to true there, beforehand. But no cursor blinking inside any of the text inputs!

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

          Hi @tomy , have you made the 2 changes inside the FocusScope ?

          Because i have just copy pasted your code and added the 2 lines which i mentioned above, when launched the default focus is on the first TextInput and when tab is pressed the focus moves to the next TextInput.

          Output:-

          0_1559828536143_e95b65cf-5a9b-41c2-8ed3-00aef7a21304-image.png

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

          tomyT 1 Reply Last reply
          5
          • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

            Hi @tomy , have you made the 2 changes inside the FocusScope ?

            Because i have just copy pasted your code and added the 2 lines which i mentioned above, when launched the default focus is on the first TextInput and when tab is pressed the focus moves to the next TextInput.

            Output:-

            0_1559828536143_e95b65cf-5a9b-41c2-8ed3-00aef7a21304-image.png

            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @Shrinidhi-Upadhyaya

            Yes, but activeFocusOnTab :true is not mentioned in the book as far as I saw, so if it's mandatory, why is it not written there!?

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

              Hi @tomy , its mentioned in the documentation[https://doc.qt.io/qt-5/qml-qtquick-item.html#activeFocusOnTab-prop]

              By default its set to false.

              0_1559832497714_de332a6b-553b-42dc-986c-361821bffee6-image.png

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

              tomyT 1 Reply Last reply
              3
              • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                Hi @tomy , its mentioned in the documentation[https://doc.qt.io/qt-5/qml-qtquick-item.html#activeFocusOnTab-prop]

                By default its set to false.

                0_1559832497714_de332a6b-553b-42dc-986c-361821bffee6-image.png

                tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by
                #7

                @Shrinidhi-Upadhyaya

                Apart from the Tab key, why don't we have a default focus while it's set?

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

                  Hi @tomy , you need to set the focus, as i see from your code you have not set the focus anywhere.

                  Everything is clearly mentioned in the documentation with examples please have look once[https://doc.qt.io/qt-5/qml-qtquick-item.html#focus-prop]

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

                  tomyT 1 Reply Last reply
                  3
                  • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                    Hi @tomy , you need to set the focus, as i see from your code you have not set the focus anywhere.

                    Everything is clearly mentioned in the documentation with examples please have look once[https://doc.qt.io/qt-5/qml-qtquick-item.html#focus-prop]

                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by tomy
                    #9

                    @Shrinidhi-Upadhyaya

                    It's previously set. Please look at TLineEditV1.qml once again:

                    ...
                    TextInput {
                            id: input
                            width: 100; height: 20
                            anchors.fill: parent
                            anchors.margins: 4
                            focus: true   // <= Here
                        }
                    ...
                    

                    You can also test it yourself.

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

                      Hi @tomy , you have set the focus to the TextInput not the FocusScope, so TextInput will gain active focus only when FocusScope gains active focus.

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

                      tomyT 1 Reply Last reply
                      5
                      • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                        Hi @tomy , you have set the focus to the TextInput not the FocusScope, so TextInput will gain active focus only when FocusScope gains active focus.

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by
                        #11

                        @Shrinidhi-Upadhyaya
                        Thank you, now solved.
                        The book sucks at times. It hasn't mentioned this.

                        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