Can't get key events when using ApplicationWindow


  • Moderators

    Ok so I finally decided it's time to learn QML. Things are going pretty well however I am having trouble with key events if I use ApplicationWindow.

    Here is my code:

    main.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.1
    
    ApplicationWindow {
        id: root
        width: 600
        height: 400
        visible: true
    
        property Component testView: TestView { }
    
        StackView {
            id: stack
            anchors.fill: parent
            initialItem: testView
        }
    }
    

    TestView.qml

    import QtQuick 2.7
    
    Item {
        id: container
        focus: true
    
        Keys.onPressed: {
            console.log("key press " + event.key)
        }
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int ac, char **av)
    {
            QGuiApplication app(ac, av);
            bool shouldExit = false;
            QQmlApplicationEngine engine;
            QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, [&shouldExit](QObject *p){ if (!p) shouldExit = true; });
            engine.load(QUrl("qrc:///qml/main.qml"));
            return (shouldExit) ? 1 : app.exec();
    }
    

    pro file:

    TEMPLATE = app
    TARGET = qml-key-test
    INCLUDEPATH += .
    QT += quick qml
    CONFIG += c++11
    
    # Input
    SOURCES += src/main.cpp
    

    qml.qrc:

    <RCC>
            <qresource prefix="/">
                    <file>qml/main.qml</file>
                    <file>qml/TestView.qml</file>
            </qresource>
    </RCC>
    

    So - if I run the TestView.qml with qmlscene everything works fine, I get keypresses logged to the console.

    However if I run it with qmlscene main.qml or via compiling and running the exe using the qml engine, the keypresses don't get caught and logged.

    I can't figure out what I'm doing wrong. I have tried messing with focus stuff, but I just can't seem to get it.

    Edit: also I can tar up the project if anyone wants to play with it and not copy/paste from the code I posted.

    Edit2: Posted full project as a tar/gz at http://www.ambershark.com/qml-key-test.tar.gz

    Thanks in advance! :)


  • Moderators

    @ambershark Well after a lot of trial and error, I finally found a focus solution:

    import QtQuick 2.7
    import QtQuick.Controls 2.1
    
    ApplicationWindow {
        id: root
        width: 600
        height: 400
        visible: true
    
        property Component testView: TestView { StackView.onActivated: stack.currentItem.forceActiveFocus() }
    
        StackView {
            id: stack
            anchors.fill: parent
            initialItem: testView
        }
    }
    

    As long as I do a stack.currentItem.forceActiveFocus() I get the key events. Weird that it wasn't being focused for me. Feel kind of hacky, but at least it works.

    I'll leave this open in case anyone wants to explain why I need to force the focus.


  • Moderators

    @ambershark set focus to true for StackView too.


  • Moderators

    @p3c0 said in Can't get key events when using ApplicationWindow:

    @ambershark set focus to true for StackView too.

    I tried that and it didn't seem to make a difference.

    This whole focus thing is really confusing me in regards to key press events.

    So even now that my TestView is getting the key presses, I went a layer deeper and added an Item to the TestView.. It now doesn't get the keypresses.

    Any advice on learning how to deal with focus for key events? Having a tough time finding good tutorials, documentation, even books on QML.


  • Moderators

    @p3c0 Ok so I removed my "forced focus" line, added the focus: true to my stack view and everything started working.

    Funny, I never tried focus: true on my StackView with the right combination of things to make it work.

    Thanks for your help, that was exactly the proper solution I was looking for, rather than my hacky one. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.