Detecting focus lost & gained events in Qt for Symbian
Article Metadata
Tested with
Devices(s): Symbian
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition, FP2
S60 5th Edition
Qt 4.6.2
S60 3rd Edition, FP2
S60 5th Edition
Qt 4.6.2
Article
Keywords: symbianEventFilter, focus
Created: User:Kbwiki
(26 Apr 2013)
Last edited: hamishwillee
(11 Oct 2012)
Overview
This article explains how to detect focus change events in Qt applications on Symbian.
Description
Qt applications need to reimplement QApplication::symbianEventFilter() in order to get notification of foreground application changes, ie. focus lost & gained events. This is needed because focusInEvent(), focusOutEvent(), or hideEvent() from the QWidget class are not called on the Symbian platform when the foreground application changes.
Solution
Define an application class that inherits QApplication and reimplement symbianEventFilter():
#include <QDebug>
#ifdef Q_OS_SYMBIAN
#include <QSymbianEvent>
#include <w32std.h>
#endif
class MyApplication : public QApplication
{
public:
MyApplication( int argc, char** argv ) : QApplication( argc, argv ) {}
#ifdef Q_OS_SYMBIAN
protected:
bool symbianEventFilter( const QSymbianEvent* symbianEvent ) {
const TWsEvent *event = symbianEvent->windowServerEvent();
if( !event ) {
return false;
}
switch( event->Type() ) {
case EEventFocusGained: {
qDebug() << "Focus gained";
break;
}
case EEventFocusLost: {
qDebug() << "Focus lost";
break;
}
default:
break;
}
// Always return false so we don't stop
// the event from being processed
return false;
}
#endif // Q_OS_SYMBIAN
};


Contents
AlterX - signal not possible in QApplication?!
Hi, i'm using that schema and i replaced qDebug with two my own signal...when i compile the application, the error:
MyBackgroundApp is a subclass of QApplication and appGoingInForeground is a signal defined in MyBackgroundApp.
Moreover if i try to connect the signal from within QML file (after i've used setContextProperty), it shows me that is undefined!AlterX 16:16, 19 August 2011 (EEST)
Hamishwillee - Tried the forums
Best to post question on the discussion boards, unless specifically about the article.
The problem is mos likely that your application is not a QObject based class, or you've omitted the Q_OBJECT macro in its declaration.hamishwillee 05:15, 22 August 2011 (EEST)
Ic000001 - How to do this in a Qt Quick application?
Hi there,
Apologies - I'm really rusty on C++ and don't know much raw Qt.
I have a Qt Quick Application "using template from Qt Creator" ... how can I implement this? (i.e. suspending Audio when my Qt Quick app is suspended)
My main.cpp:
int main(int argc, char *argv[]) {
QmlApplicationViewer viewer; viewer.setMainQmlFile(QLatin1String("qml/QtQuickApplication/main.qml")); viewer.showExpanded();}
Your help would be much appreciated!The project also has the qmlapplicationviewer.cpp and .h files.
ic000001 10:01, 22 October 2011 (EEST)
A1291762 - Re: QML Application
Ic000001, You can replace QApplication with the MyApplication class described. You just need to find a way tp have it poke into your QML (assuming you want to handle the event from within QML). Something like this:
a1291762 16:21, 26 October 2011 (EEST)
Hamishwillee - No, for QML you'd use the "Symbian" component, foreground property
http://doc.qt.nokia.com/qt-components-symbian/qml-symbian.html#foreground-prop
This changes when the app gains and loses focus.hamishwillee 07:32, 1 December 2011 (EET)