Managing keyboard events between a Win32 application and QWinMigrate

I have included the Qt dialog in a traditional Win32 application, and now I am a little puzzled by how to manage keyboard events propagating from Qt-> Win32. Is there any way to check if Qt is a “processing” of events (for example, the input goes to the editing block) and prevents the propagation of these events in the host application?

A Win32 application has its own very complex accelerator system, and when we work with built-in editing units, we usually disable accelerators manually. I have no way to do this for a Qt dialog, as this is a common widget among several applications.

Currently, I will disable host accelerators in the dialog as a whole, getting focus, but can Qt be said to prevent the spread of kbd events from editors? Ideally, without changing the QtDialogs code (although can I do this if necessary?)

+4
source share
2 answers

I have no idea if this will really work, but you can give it away:

class KeyEventPropagetionPreventer: public QObject
{
public:
    KeyEventPropagetionPreventer( QWidget * widget ) 
    : QObject( widget ), widget( widget ), instercept_events( true )
    {
        widget->installEventFilter( this )
    }

    bool eventFilter(QObject *obj, QEvent *event)
    {
        if ( intercept_events && event->type() == QEvent::KeyPress) // or other types if needed
        {
            intercept_events = false; // prevents eating your own events
            QKeyEvent new_event( *event ); // Might be that you need to implement your own copying function if the copyconstructor is disabled
            QApplication::sendEvent(this->widget, &new_event);
            instercept_events = true;
            return true;
        } 
        else 
        {
            return QObject::eventFilter(obj, event);
        }
    }

private:
    QWidget * widget;
    bool instercept_events;
}

Then you add this line where you create the dialog:

new KeyEventPropagetionPreventer( your_qt_dialog ); // will be deleted by the Qt parent/child system when the dialog is deleted.

, , . , ( qt-eventystem-only - ), QApplication:: sendEvent() .

, , !

(ps. )

+3

- , Qt "" (, )

, , Spy ++ ( Visual Studio), .

Qt, Win32, GetMessage(), :

BOOL bRet;

while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else  // you could do whatever you want with the message before it is dispatched
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}
+1

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


All Articles