Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Android backbutton
Qt 6.12 LTS is out! See what's new in the release blog

Android backbutton

Scheduled Pinned Locked Moved Mobile and Embedded
15 Posts 13 Posters 37.4k Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #2

    I think 5.1 is more like a beta suport for android. Some essential stuff like camera and full sensor support is missing (some are working though).
    I guess Qt5.2 will be awesome

    1 Reply Last reply
    0
    • F Offline
      F Offline
      flaviomarcio
      wrote on last edited by
      #3

      This is true, but I'll tell you alpha version 4.1 was better than qt5.1 for android, was more stable

      Flavio Portela

      1 Reply Last reply
      0
      • martin_kyM Offline
        martin_kyM Offline
        martin_ky
        wrote on last edited by
        #4

        Hi there. The back button functionality works nicely in the Qt 5.1 developer preview without need for any tricks and behaves "according to specification":http://developer.android.com/reference/android/app/Activity.html (scroll to onKeyDown method).

        According to Android documentation, the back button event is delivered via Activity.onKeyUp() which is translated as QEvent::KeyRelease with key code Qt::Key_Back.

        To prevent your app from quitting, you have to handle (accept) this event. For example, in QML you would do something like this:

        @
        Rectangle {
        focus: true // important - otherwise we'll get no key events

        Keys.onReleased: {
            if (event.key == Qt.Key_Back) {
                console.log("Back button captured - wunderbar !")
                event.accepted = true
            }
        }
        

        }
        @

        If you want to handle it on C++ level, override keyReleaseEvent() of your main Window and do the same in there.

        That being said, how you implement the "come back at previous stack page" is entirely up to you.

        1 Reply Last reply
        2
        • L Offline
          L Offline
          LangstoniusRex
          wrote on last edited by
          #5

          Thanks for explaining :) It is a bit odd that it somehow works its way to quit.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #6

            I managed to catch the android back button in QML:

            @ApplicationWindow {
            onClosing: {
            close.accepted = false
            if (contextMenuManager.menuVisible)
            contextMenuManager.menuVisible = false
            }
            }@

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rizzer
              wrote on last edited by
              #7

              Keys.onReleased catches the Android back button in some instances.

              It doesn't seem to work with ListView though. focus must be directed somewhere where I haven't anticipated.

              Here is a sample main.qml with a ListView, which does not catch the Android back button:
              @
              import QtQuick 2.3
              import QtQuick.Controls 1.2
              import QtQuick.Layouts 1.1

              ApplicationWindow {
              id: appWindow
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")

              ListModel {
                  id: longModel
              
                  Component.onCompleted: {
                      for ( var i=1; i<=100; i++ )
                          append({"testName": i})
                  }
              }
              
              
              ListView {
                  anchors.fill: parent
                  id: listView
                  model: longModel
              
                  delegate:
                      Rectangle {
                          height: 80
                      width: parent.width
              
                      Text {
                          id: textComponent
                          text: testName
                      }
                      Keys.onReleased: {
                          if (event.matches(StandardKey.Back)) {
                              console.log("back caught by delegate");
                              event.accepted = true;
                          }
                      }
                  }
              
                  focus: true
                  Keys.onReleased: {
                      if (event.matches(StandardKey.Back)) {
                          console.log("back caught by listview");
                          event.accepted = true;
                      }
                  }
              
              }
              

              }
              @

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rizzer
                wrote on last edited by
                #8

                Keys.onReleased catches the Android back button in some instances.

                It doesn't seem to work with ListView though. focus must be directed somewhere where I haven't anticipated.

                Here is a sample main.qml with a ListView, which does not catch the Android back button:
                @
                import QtQuick 2.3
                import QtQuick.Controls 1.2
                import QtQuick.Layouts 1.1

                ApplicationWindow {
                id: appWindow
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")

                ListModel {
                    id: longModel
                
                    Component.onCompleted: {
                        for ( var i=1; i<=100; i++ )
                            append({"testName": i})
                    }
                }
                
                
                ListView {
                    anchors.fill: parent
                    id: listView
                    model: longModel
                
                    delegate:
                        Rectangle {
                            height: 80
                        width: parent.width
                
                        Text {
                            id: textComponent
                            text: testName
                        }
                        Keys.onReleased: {
                            if (event.matches(StandardKey.Back)) {
                                console.log("back caught by delegate");
                                event.accepted = true;
                            }
                        }
                    }
                
                    focus: true
                    Keys.onReleased: {
                        if (event.matches(StandardKey.Back)) {
                            console.log("back caught by listview");
                            event.accepted = true;
                        }
                    }
                
                }
                

                }
                @

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mr_Ada
                  wrote on last edited by
                  #9

                  Can someone post a C++ version of this? I have tried to convert it and I am still not seeing the back key.

                  Never mind.... Here is a C++ version I was able to get to work....

                  @
                  #include <QDebug>

                  // regular MainWindow stuff...

                  void MainWindow::keyPressEvent (QKeyEvent* event) {

                  if (event->key () == Qt::Key_Back)
                  qDebug () << "<<keyPressEvent>> " <<
                  "[[Back button]]";
                  else if (event->key () == Qt::Key_HomePage)
                  qDebug () << "<<keyPressEvent>> " <<
                  "[[Home button]]";
                  else if (event->key () == Qt::Key_Menu)
                  qDebug () << "<<keyPressEvent>> " <<
                  "[[Menu button]]";
                  else if (event->key () == Qt::Key_unknown)
                  qDebug () << "<<keyPressEvent>> " <<
                  "[[Unknown button]]";
                  event->accept ();
                  }
                  @

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mr_Ada
                    wrote on last edited by
                    #10

                    Can someone post a C++ version of this? I have tried to convert it and I am still not seeing the back key.

                    Never mind.... Here is a C++ version I was able to get to work....

                    @
                    #include <QDebug>

                    // regular MainWindow stuff...

                    void MainWindow::keyPressEvent (QKeyEvent* event) {

                    if (event->key () == Qt::Key_Back)
                    qDebug () << "<<keyPressEvent>> " <<
                    "[[Back button]]";
                    else if (event->key () == Qt::Key_HomePage)
                    qDebug () << "<<keyPressEvent>> " <<
                    "[[Home button]]";
                    else if (event->key () == Qt::Key_Menu)
                    qDebug () << "<<keyPressEvent>> " <<
                    "[[Menu button]]";
                    else if (event->key () == Qt::Key_unknown)
                    qDebug () << "<<keyPressEvent>> " <<
                    "[[Unknown button]]";
                    event->accept ();
                    }
                    @

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      OE0214
                      wrote on last edited by
                      #11

                      on Android, I did this:

                      void MainWindow::keyPressEvent(QKeyEvent *event)
                      {
                      	if (event->key() == Qt::Key_Back)
                      		qDebug() << "[[Back button]]";
                      	else if (event->key() == Qt::Key_Menu)
                      		qDebug() << "[[Menu button]]";
                      	else if (event->key() == Qt::Key_TopMenu)
                      		qDebug() << "[[Top menu button]]";
                      	else
                      		qDebug() << "[[some other button]]";
                      
                      	event->accept();
                      }
                      

                      I get a debug message for the back button, but not for the menu button...I also tried Qt::Key_TopMenu, but still nothing

                      nor do I get the "some other button" debug message when the menu button is pressed, so this method is not even being called when I press the menu button

                      this message must be handled somewhere else

                      can anyone help?

                      Thanks

                      1 Reply Last reply
                      0
                      • uCampaignU Offline
                        uCampaignU Offline
                        uCampaign
                        wrote on last edited by
                        #12

                        In Qt 5.4.2 this is what i use and it works fine:

                        ApplicationWindow {
                        ....
                        onClosing: {
                        if (Qt.platform.os == "android") {
                        close.accepted = false;
                        if (stack.depth > 1) stack.pop();
                        }
                        }
                        }

                        S 1 Reply Last reply
                        2
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #13

                          C++ code. Gives the desired/standard effect on Android. Use a QWidget with a QStackedLayout in the main window.

                              stackedLayout = new QStackedLayout;
                              QWidget *c = new QWidget();
                              first = new FirstForm(this );
                          
                              stackedLayout->addWidget( first );
                          
                              c->setLayout( stackedLayout );
                              setCentralWidget( c );
                          

                          Override the closeEvent in the header:

                          protected:
                              void closeEvent(QCloseEvent *event);
                          

                          Implement the code in the body:

                          void MainWindow::closeEvent(QCloseEvent *event) {

                          if( stackedLayout->currentIndex() > 0 ) {
                              QWidget *top = stackedLayout->currentWidget();
                              stackedLayout->removeWidget( top );
                          
                              event->ignore();
                          }
                          

                          }

                          // QED

                          1 Reply Last reply
                          0
                          • uCampaignU uCampaign

                            In Qt 5.4.2 this is what i use and it works fine:

                            ApplicationWindow {
                            ....
                            onClosing: {
                            if (Qt.platform.os == "android") {
                            close.accepted = false;
                            if (stack.depth > 1) stack.pop();
                            }
                            }
                            }

                            S Offline
                            S Offline
                            seyed
                            wrote on last edited by
                            #14

                            @uCampaign this is my enhanced version:

                            Window {
                                //...
                                onClosing: {
                                    if (Qt.platform.os === "android" || Qt.platform.os === "ios") {
                                        close.accepted = false;
                                        if (menu.isShown) menu.hide();
                                        else if (stackView.depth > 1) stackView.pop();
                                        else Qt.quit();
                                    }
                                }
                            }
                            

                            Thank you :)

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              kgregory
                              wrote on last edited by
                              #15

                              Does anyone know of a full example of an app that captures the back button? Maybe a github repo? None of these code snippets are working for me and I'm not sure why.

                              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
                              • Unsolved