Back Button in QML

3,184 views
Skip to first unread message

PAT

unread,
Sep 3, 2013, 5:38:46 AM9/3/13
to andro...@googlegroups.com
Hello,
i have a Qt Quick 2 App. In the main.qml-file i have a "StackView" to navigate to other pages. When i press the back-button on the device, i want to navigate to the previous page, by calling StackView.pop(). Actually the StackView works really good, but i cannot catch the "Keys.onPressed"-Signal. (also tried many others, like "onBacktabPressed(KeyEvent event)" ).
Could it be, that I cannot use the event, while the device only has softkeys ( I just have the Nexus 4 and 10, and they both have just softkeys as back-button).
Does anyone had the same problem already? Is there any possibility to react on pressing the back-button in QML, without hacking all the java sources?






Message has been deleted

Максим Давиденко

unread,
Sep 10, 2013, 11:14:31 AM9/10/13
to andro...@googlegroups.com
I've solved this problem by this way. (I think this is not good idea, but nothing to do)

In my project, ./android/src/org/kde/necessitas/origo/QtActivity.java i've found method onKeyUp.
Here we need catch and forward KEYCODE_BACK. In Qml and in Qt at all we have no same keycode, so we can redefine it.
int newKeyCode = keyCode;
if (keyCode == KeyEvent.KEYCODE_BACK)
    newKeyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;
}

and now call delegate with newKeyCode arg.
Now, in qml:
Keys.OnPressed
{
   if (event.key == Qt.Key_MediaPrevious)
      PagesStack.pop();
}

Idea with keycode redefinition was published by author of qt components for android (can't remember his name)
P.S. Sorry for my english, hope this helps

понедельник, 9 сентября 2013 г., 10:34:23 UTC+4 пользователь inspect...@gmail.com написал:
I got the same problem. Any knows a workaround to use the backbutton in qml?

Daniel Ortiz Pereira da Silva

unread,
Sep 10, 2013, 1:08:30 PM9/10/13
to andro...@googlegroups.com
Try this on main.qml

Rectangle {
    width: 100
    height: 70
focus: true

    // FIXME: does not work yet!
    Keys.onPressed: {
        console.log("KEY_PRESSED: " + event.key)
        if (event.key === Qt.Key_Back) {
            event.accepted = true;
            if (sv.depth > 1) {
                sv.pop();
            } else { Qt.quit(); }
        }
    }

Works fine for me.

PAT

unread,
Sep 11, 2013, 4:34:55 AM9/11/13
to andro...@googlegroups.com
Thank you!
A combination of those solutions worked for me.
What I changed to make the back button do its job:
  • set the focus of the root-Rectangle true
  • Changed the method onKeyUp(..) in QtActivity.java as follow:
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event)
    {

        int newKeyCode = keyCode;
        if (keyCode == KeyEvent.KEYCODE_BACK)
            newKeyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;

        if (QtApplication.m_delegateObject != null  && QtApplication.onKeyDown != null)
            return (Boolean) QtApplication.invokeDelegateMethod(QtApplication.onKeyUp, newKeyCode, event);
        else
            return super.onKeyUp(keyCode, event);
    }

  • Btw, i have to use the event "Keys.onReleased:" insted of "Keys.onPressed:" and I changed the implementation like this:
    // FIXED: does now work!! :)
    Keys.onReleased: {
        console.log("KEY_PRESSED: " + event.key)
        if (event.key === Qt.Key_MediaPrevious) {
            event.accepted = true;
            if (sv.depth > 1) {
                sv.pop();
            } else { Qt.quit(); }
        }
    }



Thank you again for your help!

PAT

unread,
Nov 15, 2013, 2:30:35 AM11/15/13
to andro...@googlegroups.com
Hey,
in Qt 5.2 beta1 it worked, how i guess it is supposed to work: (no change in the java-sources needed)
Keys.onReleased: {
        if (event.key === Qt.Key_Back) {
            event.accepted = true;
            if (stackView.depth > 1) {
                stackView.pop();
            } else { Qt.quit(); }
        }
    }
But now with rc1, exits ride away, by clicking an the back button...
bug or feature? Does anyone has the same problem?

Максим Давиденко

unread,
Nov 15, 2013, 2:43:42 AM11/15/13
to andro...@googlegroups.com
Hi,

I have not used Qt 5 for android. I think you should check java sources, may be key changed (e.g. Qt.KeyBack instead of Qt.Key_Back or something else). Another way, it is posible to catch all key events without filtering, then press at Back key and dump event.key for it. Hope this helps

пятница, 15 ноября 2013 г., 10:30:35 UTC+3 пользователь PAT написал:
Message has been deleted

PAT

unread,
Nov 15, 2013, 3:45:21 AM11/15/13
to andro...@googlegroups.com
sorry. It works fine. Just forgot to set the focus to true...

Am Freitag, 15. November 2013 09:35:07 UTC+1 schrieb PAT:
Thank you.
But the the problem is, that i do not get any key event in QML.
In java I get every key event, and i can dump it. But I Think we do not change the java sources. Hope that it is just a bug, which gets fixed before release...
Reply all
Reply to author
Forward
0 new messages