PDA

View Full Version : Application Lost Focus / Inactive / In Background



javed_alam786
27th April 2011, 12:31
Hi All,

The issue i am facing is basically how do i detect my application has lost focus as a result of user doing something else maybe checking there SMS or making/receiving a call.

All i want to do is detect this focus change and 'Pause' my app from doing any heavy calculation or simply sending a message "User Inactive State" to TCP Server.

Regards,
Javed

i have tried these two solutions but none works for me, perhaps i am doing it wrong.


Solution 1:- Does not ssems to be emitting signal when app looses focus.


ProgressWin::ProgressWin(QWidget *parent) : QDialog(parent), ui(new Ui::ProgressWin)
{
ui->setupUi(this);

#ifdef Q_OS_SYMBIAN
this->showMaximized();
#else
this->show();
#endif

QWidget::setFocusPolicy(Qt::StrongFocus);
connect(this, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(OnAppFocusChanged(QWidget*, QWidget*)));
}


void ProgressWin::OnAppFocusChanged(QWidget* old, QWidget* now)
{
if(NULL == old)
{
// do something or nothing
}
else if(NULL == now)
{
Pause(); // Pause all calculation
SendTCP_UserInActive();
}
else
{
// do something or nothing
}
}


Solution 2 :- Does not work if user selects "Calendar key or Email Key" pressed on Nokia E52 Handset. Its a crappy solution anyway.


void ProgressWin::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
case Qt::Key_Calendar:
case Qt::Key_LaunchMail:
case Qt::Key_Menu:
.........
.........
.........
Pause(); // Pause all calculation
SendTCP_UserInActive();
break;
default:
// do something or nothing
}
}

MarekR22
27th April 2011, 13:05
try listen QWindowStateChangeEvent and check QWidget::windowState() value. There is Qt::WindowActive flag.

javed_alam786
28th April 2011, 11:35
Thanks MarekR22 for the direction. Problem solved. Much appreciated.

javed_alam786
10th May 2011, 10:12
Solution here :-

http://www.qtcentre.org/threads/41040-Application-Lost-Focus-Inactive-In-Background?p=189771#post189771