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. Experimenting with focus

Experimenting with focus

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 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.
  • ashajgA Offline
    ashajgA Offline
    ashajg
    wrote on last edited by
    #1

    Hi guys

    I was thinking if I take 3 rectangles and set focus for all three rectangles true so on keyboard key pressed all three rectangles should capture that event and execute code inside Keys.onPressed. But when I checked by running code only first rectangle is able to capture the key event while others are unable to... I am curious to know what is exactly happening...

    Code:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 600
        title: qsTr("Hello World")
    
    
        Rectangle
        {   id:one
            width:200
            height:200
            color:"RED"
            focus:true
    
            Keys.onPressed:
                   {
                       if(event.key === Qt.Key_Down)
                       {
                       one.color="yellow"
                       }
    
                   }
    
        }
    
        Rectangle
        {   id:two
            y:300
            width:200
            height:200
            color:"red"
            focus:true
            Keys.onPressed:
                   {
                       if(event.key === Qt.Key_Down)
                       {
                       two.color="yellow"
                       }
    
                   }
        }
    
    
        Rectangle
        {   id:three
            x:300
            width:200
            height:200
            color:"red"
            focus:true
            Keys.onPressed:
                   {
                       if(event.key === Qt.Key_Down)
                       {
                       three.color="yellow"
                       }
    
                   }
        }
    }
    

    output:

    0_1549540479683_0f16016e-4f84-4841-aa92-26609fb2321b-image.png

    How can I change the property of all three at once ?

    J.HilkJ 1 Reply Last reply
    0
    • YunusY Offline
      YunusY Offline
      Yunus
      wrote on last edited by Yunus
      #2

      @ashajg hi. the problem is about "focus: true" because of this, the other scopes are unreachable.
      maybe you can try in first "if scope" to make all yellow:

      if(event.key === Qt.Key_Down)
      {
      one.color="yellow"
      two.color="yellow"
      three.color="yellow"

                     }
      
      1 Reply Last reply
      1
      • ashajgA ashajg

        Hi guys

        I was thinking if I take 3 rectangles and set focus for all three rectangles true so on keyboard key pressed all three rectangles should capture that event and execute code inside Keys.onPressed. But when I checked by running code only first rectangle is able to capture the key event while others are unable to... I am curious to know what is exactly happening...

        Code:

        import QtQuick 2.9
        import QtQuick.Window 2.2
        
        Window {
            visible: true
            width: 640
            height: 600
            title: qsTr("Hello World")
        
        
            Rectangle
            {   id:one
                width:200
                height:200
                color:"RED"
                focus:true
        
                Keys.onPressed:
                       {
                           if(event.key === Qt.Key_Down)
                           {
                           one.color="yellow"
                           }
        
                       }
        
            }
        
            Rectangle
            {   id:two
                y:300
                width:200
                height:200
                color:"red"
                focus:true
                Keys.onPressed:
                       {
                           if(event.key === Qt.Key_Down)
                           {
                           two.color="yellow"
                           }
        
                       }
            }
        
        
            Rectangle
            {   id:three
                x:300
                width:200
                height:200
                color:"red"
                focus:true
                Keys.onPressed:
                       {
                           if(event.key === Qt.Key_Down)
                           {
                           three.color="yellow"
                           }
        
                       }
            }
        }
        

        output:

        0_1549540479683_0f16016e-4f84-4841-aa92-26609fb2321b-image.png

        How can I change the property of all three at once ?

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @ashajg
        the issue is not with focus, but with activeFocus.
        Only items that have the active focus will detect the onKey event. I don't think more than 1 Item can have the active focus.

        with setting focus to true, that item will get active focus as soon the the item is loaded. My assumption is, that because the whole scene is loaded at once, only the first item with the focus set to true will get the active focus event.

        but I may be wrong.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • ashajgA Offline
          ashajgA Offline
          ashajg
          wrote on last edited by
          #4

          Thanks @Yunus @J-Hilk

          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