How to Handle the Red End Key in a Qt Application: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Solution=
{{delete|reason=Outdated Symbian related information}}
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


= Solution =
This requires wrapping the QApplication::symbianEventFilter() function.
This requires wrapping the QApplication::symbianEventFilter() function.
<code>
Required libraries: QtGui
Required header files: w32std.h avkon.hrh
PlatSec Capabilities: None
Compatibility: S60 3.1 - Symbian^3
bool EndKeyAwareApplication::symbianEventFilter(const QSymbianEvent '''event)
{
if (event->type()  QSymbianEvent::WindowServerEvent) {
        //filtering window server events (TWsEvent)
        if (event->windowServerEvent()->Type()  KAknUidValueEndKeyCloseEvent) {
//filtering AVKON close event sent by end key
//send a Key_Hangup press/release to the focused widget
//change this section if your application needs to do something else
QObject''' target = focusWidget();
if (target) {
QEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Hangup, Qt::NoModifier);
postEvent(target, event);
QEvent *event2 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Hangup, Qt::NoModifier);
postEvent(target, event2);
}
//return true to prevent default handling of the event (default handler will quit app)
return true;
}
}
//allow base class to handle any other events in the default way
return QApplication::symbianEventFilter(event);
}</code>


This sample shows a simple way of handling the end key so that your application is not quit. It does not prevent the application being closed by the task switcher, and does not interfere with operation of the phone application. Pressing the red key will bring the phone application to the foreground, but leave your application running in the background.
This sample shows a simple way of handling the end key so that your application is not quit. It does not prevent the application being closed by the task switcher, and does not interfere with operation of the phone application. Pressing the red key will bring the phone application to the foreground, but leave your application running in the background.


===Variants and Extensions===
=== Variants and Extensions ===


To save state and exit when the red key is pressed, you should instead use the QCoreApplication::aboutToQuit signal, which is cross-platform
To save state and exit when the red key is pressed, you should instead use the QCoreApplication::aboutToQuit signal, which is cross-platform


===Summary===
=== Summary ===


This recipe shows how you handle the red End key in a Qt application.
This recipe shows how you handle the red End key in a Qt application.
Note: This article is originally available from symbian foundation wiki: http://developer.symbian.org/wiki/Handle_the_red_End_key_in_a_Qt_application . However, a backup copy has been maintained here as the site would be closed in December. Authored by Orbital <span class="caps">AND</span> Hamishwillee. Licensed under the Creative Commons Attribution-Share Alike 2.0 UK: England &amp; Wales License.

Latest revision as of 10:28, 26 May 2015

This article is nominated for deletion. Reason: Outdated Symbian related information
Please raise your support/opposition to this nomination in the article's discussion page.
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

Solution

This requires wrapping the QApplication::symbianEventFilter() function.

Required libraries: QtGui
Required header files: w32std.h avkon.hrh
PlatSec Capabilities: None
Compatibility: S60 3.1 - Symbian^3

bool EndKeyAwareApplication::symbianEventFilter(const QSymbianEvent '''event)
{
 if (event->type()  QSymbianEvent::WindowServerEvent) {
        //filtering window server events (TWsEvent)
        if (event->windowServerEvent()->Type()  KAknUidValueEndKeyCloseEvent) {
 //filtering AVKON close event sent by end key

 //send a Key_Hangup press/release to the focused widget
 //change this section if your application needs to do something else
 QObject''' target = focusWidget();
 if (target) {
 QEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Hangup, Qt::NoModifier);
 postEvent(target, event);
 QEvent *event2 = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Hangup, Qt::NoModifier);
 postEvent(target, event2);
 }
 //return true to prevent default handling of the event (default handler will quit app)
 return true;
 }
 }
 //allow base class to handle any other events in the default way
 return QApplication::symbianEventFilter(event);
}

This sample shows a simple way of handling the end key so that your application is not quit. It does not prevent the application being closed by the task switcher, and does not interfere with operation of the phone application. Pressing the red key will bring the phone application to the foreground, but leave your application running in the background.

Variants and Extensions

To save state and exit when the red key is pressed, you should instead use the QCoreApplication::aboutToQuit signal, which is cross-platform

Summary

This recipe shows how you handle the red End key in a Qt application.