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.
This commit is contained in:
Magne Sjaastad committed 2025-11-10 14:58:55 +01:00
1 parent 3d82a00f1a
commit 36965386af
3 files changed
+87 -52

No files matched your search

@@ -68,6 +68,9 @@ void TamComboBox::defineEditorAttribute( const caf::PdmFieldHandle* field,
auto attr = dynamic_cast<caf::PdmUiComboBoxEditorAttribute*>( attribute );
if ( attr )
{
attr->enableEditableContent = true;
attr->enableEditableContent = true;
attr->enableAutoComplete = false;
attr->adjustWidthToContents = true;
attr->notifyWhenTextIsEdited = false;
}
}
@@ -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
@@ -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