From 945dd574bf3f11f0c7e6b4caa4e7638b3d0b647f Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Mon, 16 Feb 2026 11:49:43 +0100 Subject: [PATCH] Improve reliability of validation error tooltips in UI Enhance validation error tooltip handling by introducing a 100 ms delay before display to prevent premature dismissal from key events. Ensure tooltips are shown at the correct widget position and only when visible. Also, improve removal of error icons and support for composite editor widgets. These changes make error feedback more consistent and user-friendly. --- .../cafPdmUiFieldEditorHandle.cpp | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp index 85a284fe2f..62ee7f4757 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmUiCore/cafPdmUiFieldEditorHandle.cpp @@ -48,6 +48,8 @@ #include #include #include +#include +#include namespace caf { @@ -338,8 +340,13 @@ void PdmUiFieldEditorHandle::applyValidationStyling( QWidget* widget, const QStr // Valid - remove error icon if present if ( m_validationErrorAction ) { - widget->removeAction( m_validationErrorAction ); + if ( auto* parentWidget = qobject_cast( m_validationErrorAction->parent() ) ) + { + parentWidget->removeAction( m_validationErrorAction ); + } delete m_validationErrorAction; + m_validationErrorAction = nullptr; + QToolTip::hideText(); } } else @@ -353,6 +360,11 @@ void PdmUiFieldEditorHandle::applyValidationStyling( QWidget* widget, const QStr // For QLineEdit, use TrailingPosition to show icon inside the widget QLineEdit* lineEdit = qobject_cast( widget ); + if ( !lineEdit ) + { + // Search for a QLineEdit child in composite editor widgets (e.g. checkbox + line edit) + lineEdit = widget->findChild(); + } if ( lineEdit ) { m_validationErrorAction = lineEdit->addAction( warningIcon, QLineEdit::TrailingPosition ); @@ -363,6 +375,27 @@ void PdmUiFieldEditorHandle::applyValidationStyling( QWidget* widget, const QStr widget->addAction( m_validationErrorAction ); } m_validationErrorAction->setToolTip( errorMessage ); + + // Defer tooltip display until after all UI updates and pending key events are processed. + // A 100 ms delay is used because Qt dismisses tooltips on any key event. When the user + // presses Enter, both a key press and key release event are generated. A 0 ms delay would + // show the tooltip between these events, and the key release would immediately dismiss it. + QWidget* tooltipWidget = lineEdit ? static_cast( lineEdit ) : widget; + if ( tooltipWidget->isVisible() ) + { + QPointer safeWidget( tooltipWidget ); + QString msg = errorMessage; + QTimer::singleShot( 100, + tooltipWidget, + [safeWidget, msg]() + { + if ( safeWidget ) + { + QPoint pos = safeWidget->mapToGlobal( QPoint( safeWidget->width(), 0 ) ); + QToolTip::showText( pos, msg, safeWidget ); + } + } ); + } } else {