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 Updated to NodeBB v4.3 + New Features

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 674 Views 1 Watching
  • 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 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?

    JoeCFDJ jeremy_kJ 2 Replies Last reply
    0
    • C Creatorczyk

      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?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @Creatorczyk
      try: appMainID.close()

      C jeremy_kJ 2 Replies Last reply
      0
      • C Creatorczyk

        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?

        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on 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
        0
        • jeremy_kJ jeremy_k

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

          jeremy_kJ 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @Creatorczyk
            try: appMainID.close()

            C Offline
            C Offline
            Creatorczyk
            wrote on last edited by
            #5

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

            JoeCFDJ 1 Reply Last reply
            0
            • C Creatorczyk

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

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on 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

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

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on 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
                • JoeCFDJ JoeCFD

                  @Creatorczyk
                  try: appMainID.close()

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on 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

                  • Login

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