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. Can't get key events when using ApplicationWindow
QtWS25 Last Chance

Can't get key events when using ApplicationWindow

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 1.9k 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.
  • A Offline
    A Offline
    ambershark
    wrote on last edited by ambershark
    #1

    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! :)

    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

    A 1 Reply Last reply
    0
    • A ambershark

      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! :)

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @ambershark set focus to true for StackView too.

        157

        A 2 Replies Last reply
        2
        • p3c0P p3c0

          @ambershark set focus to true for StackView too.

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • p3c0P p3c0

            @ambershark set focus to true for StackView too.

            A Offline
            A Offline
            ambershark
            wrote on last edited by
            #5

            @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. :)

            My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

            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