From 36965386af5aa8bb190a129e1b9b9657b4bfb26d Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 9 Oct 2025 16:33:17 +0200 Subject: [PATCH] Fixes focus out event on combo box Connects the focus out event of the combo box to a slot that sets the current text to the field. This ensures that the entered text is saved when the combo box loses focus. --- .../cafTestApplication/TamComboBox.cpp | 5 +- .../cafPdmUiComboBoxEditor.cpp | 110 ++++++++++-------- .../cafUserInterface/cafPdmUiComboBoxEditor.h | 24 ++++ 3 files changed, 87 insertions(+), 52 deletions(-) diff --git a/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.cpp b/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.cpp index 8a5c064c58..1a4f7e1f69 100644 --- a/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.cpp +++ b/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.cpp @@ -68,6 +68,9 @@ void TamComboBox::defineEditorAttribute( const caf::PdmFieldHandle* field, auto attr = dynamic_cast( attribute ); if ( attr ) { - attr->enableEditableContent = true; + attr->enableEditableContent = true; + attr->enableAutoComplete = false; + attr->adjustWidthToContents = true; + attr->notifyWhenTextIsEdited = false; } } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp index 11d504fdfc..ddf49a464b 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.cpp @@ -311,57 +311,6 @@ QMargins PdmUiComboBoxEditor::calculateLabelContentMargins() const return contentMargins; } -//-------------------------------------------------------------------------------------------------- -// Special class used to prevent a combo box to steal focus when scrolling -// the QScrollArea using the mouse wheel -// -// Based on -// http://stackoverflow.com/questions/5821802/qspinbox-inside-a-qscrollarea-how-to-prevent-spin-box-from-stealing-focus-when -//-------------------------------------------------------------------------------------------------- -class CustomQComboBox : public QComboBox -{ -public: - explicit CustomQComboBox( QWidget* parent = nullptr ) - : QComboBox( parent ) - { - } - - //-------------------------------------------------------------------------------------------------- - /// - //-------------------------------------------------------------------------------------------------- - void wheelEvent( QWheelEvent* e ) override - { - if ( hasFocus() ) - { - QComboBox::wheelEvent( e ); - } - else - { - // Ignore the event to make sure event is handled by another widget - e->ignore(); - } - } - -protected: - //-------------------------------------------------------------------------------------------------- - /// - //-------------------------------------------------------------------------------------------------- - void focusInEvent( QFocusEvent* e ) override - { - setFocusPolicy( Qt::WheelFocus ); - QComboBox::focusInEvent( e ); - } - - //-------------------------------------------------------------------------------------------------- - /// - //-------------------------------------------------------------------------------------------------- - void focusOutEvent( QFocusEvent* e ) override - { - setFocusPolicy( Qt::StrongFocus ); - QComboBox::focusOutEvent( e ); - } -}; - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -386,6 +335,7 @@ QWidget* PdmUiComboBoxEditor::createEditorWidget( QWidget* parent ) m_layout->addWidget( m_comboBox ); connect( m_comboBox, SIGNAL( activated( int ) ), this, SLOT( slotIndexActivated( int ) ) ); + connect( m_comboBox, SIGNAL( signalFocusOutEvent() ), this, SLOT( slotSetCurrentTextToField() ) ); m_autoValueToolButton = new QToolButton( m_placeholder ); m_autoValueToolButton->setCheckable( true ); @@ -445,6 +395,20 @@ void PdmUiComboBoxEditor::slotEditTextChanged( const QString& text ) this->setValueToField( text ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiComboBoxEditor::slotSetCurrentTextToField() +{ + if ( m_attributes.enableEditableContent ) + { + // Use the text directly, as the item text could be entered directly by the user + + auto text = m_comboBox->currentText(); + this->setValueToField( text ); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -483,4 +447,48 @@ void PdmUiComboBoxEditor::slotApplyAutoValue() configureAndUpdateUi( "" ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +CustomQComboBox::CustomQComboBox( QWidget* parent /*= nullptr */ ) + : QComboBox( parent ) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void CustomQComboBox::wheelEvent( QWheelEvent* e ) +{ + if ( hasFocus() ) + { + QComboBox::wheelEvent( e ); + } + else + { + // Ignore the event to make sure event is handled by another widget + e->ignore(); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void CustomQComboBox::focusInEvent( QFocusEvent* e ) +{ + setFocusPolicy( Qt::WheelFocus ); + QComboBox::focusInEvent( e ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void CustomQComboBox::focusOutEvent( QFocusEvent* e ) +{ + setFocusPolicy( Qt::StrongFocus ); + QComboBox::focusOutEvent( e ); + + emit signalFocusOutEvent(); +} + } // end namespace caf diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h index adac7873ce..9642f00bff 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiComboBoxEditor.h @@ -108,6 +108,7 @@ protected slots: void slotIndexActivated( int index ); void slotEditTextChanged( const QString& ); + void slotSetCurrentTextToField(); void slotNextButtonPressed(); void slotPreviousButtonPressed(); void slotApplyAutoValue(); @@ -127,4 +128,27 @@ private: int m_interactiveEditCursorPosition; }; +//-------------------------------------------------------------------------------------------------- +// Special class used to prevent a combo box to steal focus when scrolling +// the QScrollArea using the mouse wheel +// +// Based on +// http://stackoverflow.com/questions/5821802/qspinbox-inside-a-qscrollarea-how-to-prevent-spin-box-from-stealing-focus-when +//-------------------------------------------------------------------------------------------------- +class CustomQComboBox : public QComboBox +{ + Q_OBJECT +public: + explicit CustomQComboBox( QWidget* parent = nullptr ); + + void wheelEvent( QWheelEvent* e ) override; + +protected: + void focusInEvent( QFocusEvent* e ) override; + void focusOutEvent( QFocusEvent* e ) override; + +signals: + void signalFocusOutEvent(); +}; + } // end namespace caf