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. How to implement Qt.quit after press back button in Android QML application?
Forum Update on Monday, May 27th 2025

How to implement Qt.quit after press back button in Android QML application?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 3 Posters 672 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on 30 Aug 2023, 21:54 last edited by
    #1

    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?

    J J 2 Replies Last reply 30 Aug 2023, 22:22
    0
    • C Creatorczyk
      30 Aug 2023, 21:54

      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?

      J Offline
      J Offline
      JoeCFD
      wrote on 30 Aug 2023, 22:22 last edited by
      #2

      @Creatorczyk
      try: appMainID.close()

      C J 2 Replies Last reply 31 Aug 2023, 06:16
      0
      • C Creatorczyk
        30 Aug 2023, 21:54

        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?

        J Offline
        J Offline
        jeremy_k
        wrote on 31 Aug 2023, 00:08 last edited by
        #3

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

        Asking a question about code? http://eel.is/iso-c++/testcase/

        C 1 Reply Last reply 31 Aug 2023, 06:16
        0
        • J jeremy_k
          31 Aug 2023, 00:08

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

          C Offline
          C Offline
          Creatorczyk
          wrote on 31 Aug 2023, 06:16 last edited by
          #4

          @jeremy_k adding "()" didn't fix anything, it's still the same effect. Regarding functions, I meant the code block in "Keys.onReleased"

          J 1 Reply Last reply 31 Aug 2023, 07:06
          0
          • J JoeCFD
            30 Aug 2023, 22:22

            @Creatorczyk
            try: appMainID.close()

            C Offline
            C Offline
            Creatorczyk
            wrote on 31 Aug 2023, 06:16 last edited by
            #5

            @JoeCFD it didn't change anything, maybe it's because something needs to be added on the Backend?

            J 1 Reply Last reply 1 Sept 2023, 13:43
            0
            • C Creatorczyk
              31 Aug 2023, 06:16

              @jeremy_k adding "()" didn't fix anything, it's still the same effect. Regarding functions, I meant the code block in "Keys.onReleased"

              J Offline
              J Offline
              jeremy_k
              wrote on 31 Aug 2023, 07:06 last edited by
              #6

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

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • C Creatorczyk
                31 Aug 2023, 06:16

                @JoeCFD it didn't change anything, maybe it's because something needs to be added on the Backend?

                J Offline
                J Offline
                JoeCFD
                wrote on 1 Sept 2023, 13:43 last edited by
                #7

                @Creatorczyk What is your Android version? My call works for Android 11, 12 and 13. My app is built in Linux.

                1 Reply Last reply
                0
                • J JoeCFD
                  30 Aug 2023, 22:22

                  @Creatorczyk
                  try: appMainID.close()

                  J Offline
                  J Offline
                  jeremy_k
                  wrote on 1 Sept 2023, 22:48 last edited by
                  #8

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

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  0

                  1/8

                  30 Aug 2023, 21:54

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved