Hi,
Back button exits the App by default, this is the right behavior!
Of course because I question everything and also because I trust no 1,
I'll check if BogDan is crazy or not :)
- First I'll check what is the default behavior for any Android
Activity, then soon I'll discover that BogDan is not crazy
http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
!
- Then I'll ask myself if Android is the only O.S. which has this
behavior? No it is not! Because if I press Alt+F4 on my Linux, or
Windows my application exists, so, Android is not the only O.S. with
this behavior.
- Then I'll ask myself, how can I prevent my application to close on
Linux? The easiest way is to search on net, I just search for "How can
I catch Alt+F4 in my Qt application" and I choose the first link:
http://qt-project.org/faq/answer/how_can_i_catch_altf4_in_my_qt_application
. I'll try the same thing on Android and I'll be extremely surprised
to see that it works !
- But hey, I want more ! I want to catch the back button event, then
I'll start to search if I can handle it, because there are not too
many information on this topic, I'll check if someone else reported
this problem on a bug tracker, then after a while I'll discover that
somebody reported it and BogDan closed it
http://sourceforge.net/p/necessitas/tickets/76/
. Because the issue lacks basic information (I must confess that this
is indeed BogDan's fault) I'll try to check the patch
https://projects.kde.org/projects/playground/mobile/necessitas/android-qt/repository/revisions/6dc070ff2715fd75e0e8576aa6f4c524b824ad42
, for sure the patch should have enough info on this topic after
checking a few files from that patch I'll end up to this one
https://projects.kde.org/projects/playground/mobile/necessitas/android-qt/repository/diff/src/gui/kernel/qapplication_qpa.cpp?rev=6dc070ff2715fd75e0e8576aa6f4c524b824ad42&type=sbs
, at line 878 it seems that BogDan changed Qt to send a QKeyEvent with
(e->keyType == QEvent::KeyRelease && e->key == Qt::Key_Close) (check
line 859), then I'll try to overwrite keyReleaseEvent (
http://doc-
snapshot.qt-project.org/4.8/qwidget.html#keyReleaseEvent) and if the
"event->key()==Qt::Key_Close" then it means that I need to accept that
event. So I'll end up with something like this:
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
static bool accepted=true;
if (event->key()==Qt::Key_Close)
{
// do something useful here
event->setAccepted(accepted); // dont't close my Top Level
Widget !
accepted=false;// close the app next time when the user press
back button
}
}
After I'll check that everything works as I expected, then, of course
because I'm a good guy I'll share my findings with everybody on
android-qt mailing list ;-)
Cheers,
BogDan.