c++ - How do I detect application Level Focus-In in Qt 4.4.1? -


i need determine when qt 4.4.1 application receives focus.

i have come 2 possible solutions, both don’t work like.

in first possible solution, connect focuschanged() signal qapp slot. in slot check ‘old’ pointer. if ‘0’, know we’ve switched application, , want. seems reliable method of getting application detect focus in of 2 solutions presented here, suffers problem described below.

in second possible solution, overrode ‘focusinevent()’ routine, , want if reason ‘activewindowfocusreason’.

in both of these solutions, code executed @ times when don’t want be.

for example, have code overrides focusinevent() routine:

void applicationwindow::focusinevent( qfocusevent* p_event ) {    qt::focusreason reason = p_event->reason();    if( reason == qt::activewindowfocusreason &&        hasnewupstreamdata() )   {     switch( qmessagebox::warning( this, "new upstream data found!",                                   "new upstream data exists!\n"                                   "do want refresh simulation?",                                   "&yes", "&no", 0, 0, 1 ) )     {      case 0: // yes       refreshsimulation();       break;     case 1: // no       break;     }   } } 

when gets executed, qmessagebox dialog appears. however, when dialog dismissed pressing either ‘yes’ or ‘no’, function gets called again because suppose focus changed application window @ point activewindowfocusreason. don’t want happen.

likewise, if user using application opening & closing dialogs , windows etc, don’t want routine activate. note: i’m not sure of circumstances when routine activated though since i’ve tried bit, , doesn’t happen windows & dialogs, though happen @ least 1 shown in sample code.

i want activate if application focussed on outside of application, not when main window focussed in other dialog windows.

is possible? how can done?

thanks information, since important our application do.

raymond.

i think need track qevent::applicationactivate event.

you can put event filter on qapplication instance , it.

bool applicationwindow::eventfilter( qobject * watched, qevent * event ) {     if ( watched != qapp )         goto finished;      if ( event->type() != qevent::applicationactivate )         goto finished;      // invariant: looking @ application activate event     //            application object     if ( !hasnewupstreamdata() )         goto finished;      qmessagebox::standardbutton response =             qmessagebox::warning( this, "new upstream data found!",                                   "new upstream data exists!\n"                                   "do want refresh simulation?",                                   qmessagebox::yes | qmessagebox::no) );      if ( response == qmessagebox::yes )       refreshsimulation();  finished:     return <the-superclass-here>::eventfilter( watched, event ); }  applicationwindow::applicationwindow(...) {     if (qapp)         qapp->installeventfilter( );     ... } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -