QAxWidget steals focus from client application

84 Views Asked by At

Calling a functions that changes the GUI of an QAxWidget makes the caller loose focus. How can I avoid this?

1

There are 1 best solutions below

0
jaba On BEST ANSWER

Certain actions make Windows transfer the active window state to another window / application. When you trigger one of them your ActiveX application becomes active and therefore you loose focus in your client application.

To avoid this use can write a scope guard that resets the focus to your client after Windows has wrongly transferred the active state:

class AxFocusScopeGuard
{
public:
    AxFocusScopeGuard(QWidget* parentWidget)
        : m_focusedWidget(parentWidget->focusWidget())
    { }

    ~AxFocusScopeGuard()
    {
        QCoreApplication::processEvents();
        if(m_focusedWidget)
            m_focusedWidget->setFocus();
    }

private:
    QWidget* m_focusedWidget = nullptr;
};


void ActiveXCallHandler::updateGuiActiveX()
{
    AxFocusScopeGuard guard(m_parentWidget);
    axWidget->dynamicCall("updateYourGui()");
}