I need to determine when a Qt 4.4.1 application receives focus.
I came up with two possible solutions, but both of them do not work as I would like.
In the first possible solution, I connect the focusChanged () signal from qApp to SLOT. In the slot, I check the "old pointer". If it is 0, then I know that we switched to this application, and I do what I want. This is apparently the most reliable way to get the application to detect focus in the two solutions presented here, but suffers from the problem described below.
In a second possible solution, I redefine the focusInEvent () procedure and do what I want if the reason is "ActiveWindowFocusReason".
In both of these solutions, code is executed when I donβt want it to be.
For example, I have this code that overrides the focusInEvent () procedure:
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 you want to refresh this simulation?",
"&Yes", "&No", 0, 0, 1 ) )
{
case 0:
refreshSimulation();
break;
case 1:
break;
}
}
}
When this is done, the QMessageBox dialog box appears. However, when the dialog is rejected by clicking either βyesβ or βnoβ, this function is immediately called again, because I believe that the focus again turned to the application window at this point using ActiveWindowFocusReason. Obviously, I do not want this to happen.
Similarly, if the user uses the dialog boxes for opening and closing the application, etc., I do not want this routine to be activated. NOTE. I am not sure of the circumstances when this procedure is activated, although since Ive worked a little, and this does not happen for all windows and dialogs, although this happens, at least for the code shown in the example.
, , , .
? ?
, .
Raymond.