View Full Version : Check If Application now has focus again

23rd June 2011, 15:22
Good day
I would like to check if my application windows has received focus again.
an example.

If a user minimizes my window the selects it again.
or if another window was active the mine was selected again.

I hope this all makes sense

Donovan Hoare

Santosh Reddy
24th June 2011, 07:42
Check this, QWidget * QApplication::activeWindow ();

24th June 2011, 08:34
Thanbsk very much for the right direction. but how would i use this in code.

Could you perhaps please supply more information

8th July 2011, 15:30
Check this, QWidget * QApplication::activeWindow ();

Does anyone know how i would actually use this code.

8th July 2011, 15:50
Maybe we need more information
from you! ;)

Do you want to trigger some action if the focus returns?

8th July 2011, 16:28
Maybe we need more information
from you! ;)

Do you want to trigger some action if the focus returns?

Well i have an qt app. and my users have atendency to open up other applications like word or outlook etc. I wouldlike to run a function that does a test when a user sets focus to the qt app.

Example my app is called app1 and the user is using it, then an email comes in he then reads the email in outlokk then when he clicks the toolbar to reactivate my window i would like to run a function

I hope this makes sence

If not let me know what more info you need

9th July 2011, 06:13
You could try one of these alternatives, but I have no idea whether you will get a FocusIn event for *every* change of focus between all the child widgets in your app, or only if the app itself gains focus (as you would like). Try it and see if it works.

1 - Install an eventFilter on your QApplication instance and look for FocusIn events.
2 - Derive a new class from QApplication, and reimplement the event() method.

In either case, be sure you pass the event along for further processing.

Edit: The above does not work. However, this does:

// MyApp.h

#include <QtGui/QApplication>
#include <QEvent>
#include <QtDebug>

class MyApp : public QApplication

MyApp( int argc, char * argv[] )
: QApplication( argc, argv )

bool event( QEvent * pEvent )
if ( pEvent->type() == QEvent::ApplicationActivate )
qDebug() << "ApplicationActivate";
else if ( pEvent->type() == QEvent::ApplicationDeactivate )
qDebug() << "ApplicationDeactivate";
return QApplication::event( pEvent );

// main.cpp

#include "MyApp.h"
#include <QWidget>

int main(int argc, char *argv[])
MyApp a(argc, argv);
QWidget w;
return a.exec();

The qDebug() message is printed every time the app window is activated or deactivated. If you run up the app, "ApplicationActivate" prints as soon as the app starts up, then if you click on another app (like Firefox) "ApplicationDeactivate" prints. Click back on the app, it prints "ApplicationActivate" again, etc. Finally, when you close the app, "ApplicationDeactivate" prints.

This is an interesting idea; if you wanted to determine how much time a user spent using your app, this is one way to do it.

21st July 2011, 07:20
Thanks so much. this works great. you are a star and im sure this will help many people in the future