From 9e35bb92c020c524a50177497906817da1df2145 Mon Sep 17 00:00:00 2001 From: Graham Dawes Date: Mon, 1 Jul 2019 08:20:05 +0100 Subject: [PATCH] Avoid focus loss event when wxComboBox popup is opened in wxQt Prevent wxComboBox from generating a wxEVT_KILL_FOCUS event when the user opens the drop-down list under wxQt, as logically the drop-down is part of wxComboBox, even if it's a separate window at Qt level. Closes https://github.com/wxWidgets/wxWidgets/pull/1377 --- include/wx/qt/combobox.h | 1 + src/qt/combobox.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/wx/qt/combobox.h b/include/wx/qt/combobox.h index c5bce191de..75e8ee8852 100644 --- a/include/wx/qt/combobox.h +++ b/include/wx/qt/combobox.h @@ -80,6 +80,7 @@ public: virtual void Popup(); virtual void Dismiss(); + virtual bool QtHandleFocusEvent(QWidget *handler, QFocusEvent *event) wxOVERRIDE; protected: // From wxTextEntry: diff --git a/src/qt/combobox.cpp b/src/qt/combobox.cpp index 9189d068fa..af5846384d 100644 --- a/src/qt/combobox.cpp +++ b/src/qt/combobox.cpp @@ -15,6 +15,9 @@ #include #include +#include +#include +#include class wxQtComboBox : public wxQtEventSignalHandler< QComboBox, wxComboBox > { @@ -254,6 +257,22 @@ void wxComboBox::Dismiss() static_cast(GetHandle())->hidePopup(); } +bool wxComboBox::QtHandleFocusEvent(QWidget *handler, QFocusEvent *event) +{ + if ( !event->gotFocus() ) + { + // Qt treats the combobox and its drop-down as distinct widgets, but in + // wxWidgets they're both part of the same control, so we have to avoid + // generating a lose focus event if the combobox or its drop-down still + // have focus. + QWidget* const widget = qApp->focusWidget(); + if ( widget == m_qtComboBox || widget == m_qtComboBox->view() ) + return false; + } + + return wxChoice::QtHandleFocusEvent(handler, event); +} + void wxComboBox::Clear() { if ( !IsReadOnly() )