How to determine the focus focus application level in Qt 4.4.1?

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: // Yes
      refreshSimulation();
      break;
    case 1: // No
      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.

+3
3

, QEvent:: ApplicationActivate.

QApplication, .

bool
ApplicationWindow::eventFilter( QObject * watched, QEvent * event )
{
    if ( watched != qApp )
        goto finished;

    if ( event->type() != QEvent::ApplicationActivate )
        goto finished;

    // Invariant: we are now looking at an application activate event for
    //            the application object
    if ( !hasNewUpstreamData() )
        goto finished;

    QMessageBox::StandardButton response =
            QMessageBox::warning( this, "New Upstream Data Found!",
                                  "New upstream data exists!\n"
                                  "Do you want to refresh this simulation?",
                                  QMessageBox::Yes | QMessageBox::No) );

    if ( response == QMessageBox::Yes )
      refreshSimulation();

finished:
    return <The-Superclass-here>::eventFilter( watched, event );
}

ApplicationWindow::ApplicationWindow(...)
{
    if (qApp)
        qApp->installEventFilter( this );
    ...
}
+7

, . , , . . , , , . . , , .

+1

Qt-, , - , , , , , .

I assume that QApplication :: focusChanged does not work the way you want, because some widgets do not accept keyboard events, so they also return null as the "old" widget even when the focus changes inside the same application.

I am wondering if you can do anything with QApplication :: activeWindow ()

Returns the top-level window of the application with keyboard input focus, or 0 if the application window has no focus. Note that there may be activeWindow (), even if focusWidget () is missing, for example, if no widget in this window accepts key events.

0
source

Source: https://habr.com/ru/post/1696865/


All Articles