Results 1 to 8 of 8

Thread: Check If Application now has focus again

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Check If Application now has focus again

    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

    Regards
    Donovan Hoare

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Check If Application now has focus again

    Check this, QWidget * QApplication::activeWindow ();

  3. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Check If Application now has focus again

    Thanbsk very much for the right direction. but how would i use this in code.

    Could you perhaps please supply more information

  4. #4
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Check If Application now has focus again

    Quote Originally Posted by Santosh Reddy View Post
    Check this, QWidget * QApplication::activeWindow ();
    Does anyone know how i would actually use this code.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check If Application now has focus again

    Maybe we need more information
    from you!

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

  6. #6
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Check If Application now has focus again

    Quote Originally Posted by moviemax View Post
    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

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Check If Application now has focus again

    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:

    Qt Code:
    1. // MyApp.h
    2.  
    3. #include <QtGui/QApplication>
    4. #include <QEvent>
    5. #include <QtDebug>
    6.  
    7. class MyApp : public QApplication
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MyApp( int argc, char * argv[] )
    13. : QApplication( argc, argv )
    14. {}
    15.  
    16. bool event( QEvent * pEvent )
    17. {
    18. if ( pEvent->type() == QEvent::ApplicationActivate )
    19. qDebug() << "ApplicationActivate";
    20. else if ( pEvent->type() == QEvent::ApplicationDeactivate )
    21. qDebug() << "ApplicationDeactivate";
    22. return QApplication::event( pEvent );
    23. }
    24. };
    25.  
    26. // main.cpp
    27.  
    28. #include "MyApp.h"
    29. #include <QWidget>
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. MyApp a(argc, argv);
    34. w.show();
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 9th July 2011 at 05:37.

  8. The following user says thank you to d_stranz for this useful post:

    ShapeShiftme (21st July 2011)

  9. #8
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Check If Application now has focus again

    Thanks so much. this works great. you are a star and im sure this will help many people in the future

    Regards

Similar Threads

  1. Check if application is running in simulator
    By lyhoanghai in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2010, 12:38
  2. QModelIndex and check if a row sibling has focus
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2010, 05:39
  3. Replies: 5
    Last Post: 5th June 2009, 10:07
  4. How detective lost focus application ?
    By Torsten in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 14:19
  5. removing focus of application?
    By triperzonak in forum Qt Programming
    Replies: 0
    Last Post: 16th July 2008, 15:17

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.