How to implement Qt.quit after press back button in Android QML application?
-
Hi,
I would like quit my application when Back button has been pressed by user. So in my QML Android application I wrote code:
main.qml
ApplicationWindow { id: appMainID visible: true title: qsTr("Tabs") signal closeWindow Item { id: myItem focus: true Keys.onReleased: { if(event.key === Qt.Key_Back) { event.accepted = true; Qt.quit } } } }
main.cpp
CustomEngine engine; QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit); engine.importPathList(); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl){ QGuiApplication::exit(-1); } }, Qt::QueuedConnection);
However, after pressing the button, the function is entered, but the application is not closed. How can I fix it so that the application closes?
-
Hi,
I would like quit my application when Back button has been pressed by user. So in my QML Android application I wrote code:
main.qml
ApplicationWindow { id: appMainID visible: true title: qsTr("Tabs") signal closeWindow Item { id: myItem focus: true Keys.onReleased: { if(event.key === Qt.Key_Back) { event.accepted = true; Qt.quit } } } }
main.cpp
CustomEngine engine; QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit); engine.importPathList(); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl){ QGuiApplication::exit(-1); } }, Qt::QueuedConnection);
However, after pressing the button, the function is entered, but the application is not closed. How can I fix it so that the application closes?
@Creatorczyk
try: appMainID.close() -
Hi,
I would like quit my application when Back button has been pressed by user. So in my QML Android application I wrote code:
main.qml
ApplicationWindow { id: appMainID visible: true title: qsTr("Tabs") signal closeWindow Item { id: myItem focus: true Keys.onReleased: { if(event.key === Qt.Key_Back) { event.accepted = true; Qt.quit } } } }
main.cpp
CustomEngine engine; QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit); engine.importPathList(); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl){ QGuiApplication::exit(-1); } }, Qt::QueuedConnection);
However, after pressing the button, the function is entered, but the application is not closed. How can I fix it so that the application closes?
@Creatorczyk said in How to implement Qt.quit after press back button in Android QML application?:
Hi,
I would like quit my application when Back button has been pressed by user. So in my QML Android application I wrote code:
main.qml
Keys.onReleased: { if(event.key === Qt.Key_Back) { event.accepted = true; Qt.quit } }
Qt.quit is missing the () to make it a function call.
However, after pressing the button, the function is entered
Which function?
-
@Creatorczyk said in How to implement Qt.quit after press back button in Android QML application?:
Hi,
I would like quit my application when Back button has been pressed by user. So in my QML Android application I wrote code:
main.qml
Keys.onReleased: { if(event.key === Qt.Key_Back) { event.accepted = true; Qt.quit } }
Qt.quit is missing the () to make it a function call.
However, after pressing the button, the function is entered
Which function?
@jeremy_k adding "()" didn't fix anything, it's still the same effect. Regarding functions, I meant the code block in "Keys.onReleased"
-
@Creatorczyk
try: appMainID.close()@JoeCFD it didn't change anything, maybe it's because something needs to be added on the Backend?
-
@jeremy_k adding "()" didn't fix anything, it's still the same effect. Regarding functions, I meant the code block in "Keys.onReleased"
@Creatorczyk said in How to implement Qt.quit after press back button in Android QML application?:
@jeremy_k adding "()" didn't fix anything
Minus a future problem anyway.
Regarding functions, I meant the code block in "Keys.onReleased"
How are you checking? Have you verified that the key code matches Key_Back? Does the same code work for a desktop application build?
An up-to-date code sample would help.
-
@JoeCFD it didn't change anything, maybe it's because something needs to be added on the Backend?
@Creatorczyk What is your Android version? My call works for Android 11, 12 and 13. My app is built in Linux.
-
@Creatorczyk
try: appMainID.close()@JoeCFD said in How to implement Qt.quit after press back button in Android QML application?:
@Creatorczyk
try: appMainID.close()That looks like an attempt to trigger the QGuiApplication::quitOnLastWindowClosed behavior.