How to implement Qt.quit after press back button in Android QML application?
-
wrote on 30 Aug 2023, 21:54 last edited by
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?
wrote on 30 Aug 2023, 22:22 last edited by@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?
wrote on 31 Aug 2023, 00:08 last edited by@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?
wrote on 31 Aug 2023, 06:16 last edited by@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()wrote on 31 Aug 2023, 06:16 last edited by@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"
wrote on 31 Aug 2023, 07:06 last edited by@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?
wrote on 1 Sept 2023, 13:43 last edited by@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()wrote on 1 Sept 2023, 22:48 last edited by@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.
1/8