Pushing the Android button backwards does not start keys.onreleased qml

I am creating a program in Qt5.3 and Qtquick2.1. I am trying to capture a button click on an android in my code using Keys.onReleased. But this event does not fire. I also set the focus of the element to true. But there is still no success. Here is a sample code

import QtQuick 2.1 import QtQuick.Controls 1.2 import QtQuick.Controls.Styles 1.2 import QtQuick.Layouts 1.1 import QtQuick.Window 2.1 Rectangle { id: main2 focus: true width: Screen.Width height: Screen.Height Keys.enabled: true Keys.priority: Keys.BeforeItem property string load_page: "" signal deskConnected() Loader{ id: pageloader anchors.fill: parent source: "qrc:/qml/resources/Firstpage.qml" } onDeskConnected: { pageloader.item.onDeskConnected() } function loadPatwin(){ pageloader.source = "qrc:/qml/resources/Secondpage.qml"; } Keys.onReleased: { console.log("back"); if (event.key === Qt.Key_Back) { event.accepted=true; } } } 

Here, loadPatwin is a function called when a button is pressed, which is defined in some other qml. And loads the new qml. But after that, when I click the "Back" button on Android, the application closes, and it does not even print "back" in the logs. Any suggestions on what I'm doing wrong here?

Thanks in advance.

+4
source share
4 answers

In qt quick (Qt5.1 and later), ApplicationWindow and Window both have a closing signal that emits when the user touches the back button in android. You can simply implement the onClosing handler and set the close parameter to false. Here's how to do it:

 onClosing: { close.accepted = false } 

With this handler, you can easily control the back button of an Android device. This does not require additional permission. For example, suppose you have a StackView for managing the GUI. You can write these lines of code. Then your application will look like a native Android application:

 onClosing: { if(stack.depth > 1){ close.accepted = false stack.pop(); }else{ return; } } 
+9
source

It works for me by adding "forceActiveFocus ()" after the item has finished loading.

In your example, I would put it at the beginning. Like this:

 Rectangle { id: main2 focus: true Component.onCompleted: { main2.forceActiveFocus() } 
+5
source

I don't know if this is a good example, but I used to create my own subclass of the GuiApplication class from QGuiApplication

 #ifndef GUIAPPLICATION_H #define GUIAPPLICATION_H #include <QGuiApplication> class GuiApplication : public QGuiApplication { Q_OBJECT public: #ifdef Q_QDOC explicit GuiApplication(int &argc, char **argv); #else explicit GuiApplication(int &argc, char **argv, int = ApplicationFlags); #endif bool notify(QObject *receiver, QEvent *event); signals: void back(); }; #endif // GUIAPPLICATION_H 

This is for cpp codes

 #include "guiapplication.h" #include <QDebug> GuiApplication::GuiApplication(int &argc, char **argv, int) : QGuiApplication(argc, argv) { } bool GuiApplication::notify(QObject *receiver, QEvent *event) { // to intercept android back button #ifdef Q_OS_ANDROID if(event->type() == QEvent::Close) { emit back(); return false; } else { return QGuiApplication::notify(receiver,event); } #else return QGuiApplication::notify(receiver,event); #endif } 

For main.cpp

 #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "guiapplication.h" int main(int argc, char *argv[]) { // QGuiApplication app(argc, argv); GuiApplication app(argc, argv); QQmlApplicationEngine engine; QQmlContext *rootContext = engine.rootContext(); rootContext->setContextProperty("GUI", &app); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); } 

and finally for main.qml

 import QtQuick 2.3 import QtQuick.Controls 1.2 ApplicationWindow { id: applicationWindow visible: true width: 360 height: 360 Connections { target: GUI onBack: console.log("back") } } 
+1
source

In QML === boolean comparison

So, instead of (event.key === Qt.Key_Back)

You should use (event.key == Qt.Key_Back) {

-1
source

Source: https://habr.com/ru/post/1215045/


All Articles