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. unable to detect key press in StackView

unable to detect key press in StackView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 2.6k 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.
  • M Offline
    M Offline
    m_andrej
    wrote on last edited by m_andrej
    #1

    Keyboard event handling totally doesn't work as described in the docs: http://doc.qt.io/qt-5/qml-qtquick-keys.html

    This program does nothing when Return is pressed:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Window 2.2
    
    Window {
        visible: true
    
        StackView {
            anchors.fill: parent
            initialItem: Rectangle {
                color: "lightblue"
                focus: true
                Keys.onReturnPressed:
                    console.log("return")
            }
        }
    }
    

    However, it does work when I move the key handling code out of initialItem:

    Window {
        visible: true
    
        StackView {
            anchors.fill: parent
            initialItem: Rectangle {
                color: "lightblue"
            }
            focus: true
            Keys.onReturnPressed:
                console.log("return")
        }
    }
    

    Any ideas why?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi,
      keyboard event handling depends on the focussed element. If you want to press Return anywhere in your StackView, your second approach is the right one.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        m_andrej
        wrote on last edited by
        #3

        OK, so why isn't the light blue Rectangle focused when I set its focus property to true?

        The second approach isn't useful because I need to process key events inside the items which are pushed to StackView. The key handling depends on the currently pushed item.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          You should succeed with forceActiveFocus() on the currentItem:

              onCurrentItemChanged: {
                if (currentItem) {
                  currentItem.forceActiveFocus()
                }
              }
          
          1 Reply Last reply
          3
          • jpnurmiJ Offline
            jpnurmiJ Offline
            jpnurmi
            wrote on last edited by
            #5

            One handy way to investigate focus issues is to output Window::activeFocusItem to see where the focus is.

            Window {
                onActiveFocusItemChanged: console.log(activeFocusItem)
            }
            

            You'll notice that it outputs "qml: QQuickRootItem(0x15af1d48)", that is, the root item of the window. StackView is a focus scope, so it seems to help to say "focus: true" for both, the StackView and the Rectangle. When StackView itself has focus, the focus will be automatically transferred to the initial item.

            import QtQuick 2.5
            import QtQuick.Controls 1.4
            import QtQuick.Window 2.2
            
            Window {
                visible: true
            
                StackView {
                    focus: true
                    anchors.fill: parent
                    initialItem: Rectangle {
                        color: "lightblue"
                        focus: true
                        Keys.onReturnPressed: console.log("return")
                    }
                }
            }
            
            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