Results 1 to 3 of 3

Thread: Application Lost Focus / Inactive / In Background

  1. #1
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application Lost Focus / Inactive / In Background

    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


    Added after 25 minutes:


    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.
    Qt Code:
    1. ProgressWin::ProgressWin(QWidget *parent) : QDialog(parent), ui(new Ui::ProgressWin)
    2. {
    3. ui->setupUi(this);
    4.  
    5. #ifdef Q_OS_SYMBIAN
    6. this->showMaximized();
    7. #else
    8. this->show();
    9. #endif
    10.  
    11. QWidget::setFocusPolicy(Qt::StrongFocus);
    12. connect(this, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(OnAppFocusChanged(QWidget*, QWidget*)));
    13. }
    14.  
    15.  
    16. void ProgressWin::OnAppFocusChanged(QWidget* old, QWidget* now)
    17. {
    18. if(NULL == old)
    19. {
    20. // do something or nothing
    21. }
    22. else if(NULL == now)
    23. {
    24. Pause(); // Pause all calculation
    25. SendTCP_UserInActive();
    26. }
    27. else
    28. {
    29. // do something or nothing
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 



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

    Qt Code:
    1. void ProgressWin::keyPressEvent(QKeyEvent *e)
    2. {
    3. switch (e->key())
    4. {
    5. case Qt::Key_Calendar:
    6. case Qt::Key_LaunchMail:
    7. case Qt::Key_Menu:
    8. .........
    9. .........
    10. .........
    11. Pause(); // Pause all calculation
    12. SendTCP_UserInActive();
    13. break;
    14. default:
    15. // do something or nothing
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by javed_alam786; 27th April 2011 at 12:14.

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application Lost Focus / Inactive / In Background

    QApplication class has a static method : QWidget * QApplication::focusWidget ().

    Since it returns 0 if no widget in this application has the focus,it might be useful for what you're trying to achieve.

    So your main.cpp might look like this:
    Qt Code:
    1. QApplication app( argc,argv );
    2.  
    3. ProgressWin mainWindow;
    4.  
    5. ...
    6.  
    7. connect( &app , SIGNAL( focusChanged( QWidget*, QWidget* ) ) , &mainWindow , SLOT( OnAppFocusChanged() ) );
    8.  
    9. ...
    To copy to clipboard, switch view to plain text mode 

    And then the slot itslef:
    Qt Code:
    1. void ProgressWin :: OnAppFocusChanged()
    2. {
    3.  
    4. if( !QApplication::focusWidget() )
    5. {
    6.  
    7. // pause or whatever action you'd like to perform
    8.  
    9. }
    10.  
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Application Lost Focus / Inactive / In Background

    Hi Alex,

    Thanks for the solution. I managed to get it done a different way which works fine according to my need. This solution is open to critisim.

    Kind Regards,
    Javed

    className.h
    Qt Code:
    1. ....
    2. bool eventFilter(QObject *object, QEvent *event);
    3. ....
    To copy to clipboard, switch view to plain text mode 

    className.cpp
    Qt Code:
    1. bool ClassName::eventFilter(QObject *object, QEvent *event)
    2. {
    3. if (event->type() == QEvent::FocusIn)
    4. {
    5. // pause or whatever action you'd like to perform
    6. }
    7. else if (event->type() == QEvent::FocusOut)
    8. {
    9. // pause or whatever action you'd like to perform
    10. }
    11. else if (event->type() == QEvent::WindowDeactivate)
    12. {
    13. // pause or whatever action you'd like to perform
    14. }
    15. else
    16. {
    17. // pause or whatever action you'd like to perform
    18. }
    19. return false;
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. making Transparent window inactive on loose focus
    By Beena in forum Qt Programming
    Replies: 0
    Last Post: 17th November 2010, 06:01
  2. Replies: 0
    Last Post: 11th May 2010, 10:21
  3. QMainWindow how to know when focus lost?
    By arkaha in forum Newbie
    Replies: 1
    Last Post: 12th February 2010, 06:26
  4. Focus lost
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2010, 22:07
  5. How detective lost focus application ?
    By Torsten in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 14:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.