From efcd78777759eb63224dae7a51352026fe5652df Mon Sep 17 00:00:00 2001 From: Pieter Hijma Date: Tue, 1 Apr 2025 12:25:44 +0200 Subject: [PATCH] [Core] Fix value field unit selection Qt automatically selects the text in the value field on creation. This interferes with the selection when the field has focus. This commit ensures that the automatic selection is undone. --- src/Gui/Dialogs/DlgAddPropertyVarSet.cpp | 17 ++++++++++++++++- src/Gui/Dialogs/DlgAddPropertyVarSet.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp b/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp index a974ac4fdf..6a654898bf 100644 --- a/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp +++ b/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp @@ -153,7 +153,7 @@ static void printFocusChain(QWidget *widget) { QWidget* start = widget; int i = 0; do { - FC_ERR(" " << widget->objectName().toStdString(); + FC_ERR(" " << widget->objectName().toStdString()); widget = widget->nextInFocusChain(); i++; } while (widget != nullptr && i < 30 && start != widget); @@ -245,12 +245,24 @@ static PropertyEditor::PropertyItem *createPropertyItem(App::Property *prop) return item; } +void DlgAddPropertyVarSet::removeSelectionEditor() +{ + // If the editor has a lineedit, then Qt selects the string inside it when + // the editor is created. This interferes with the editor getting focus. + // For example, units will then be selected as well, whereas this is not + // the behavior we want. We therefore deselect the text in the lineedit. + if (auto lineEdit = editor->findChild()) { + lineEdit->deselect(); + } +} + void DlgAddPropertyVarSet::addEditor(PropertyEditor::PropertyItem* propertyItem, [[maybe_unused]]std::string& type) { editor.reset(propertyItem->createEditor(this, [this]() { this->valueChanged(); })); + editor->blockSignals(true); propertyItem->setEditorData( editor.get(), propertyItem->data(PropertyEditor::PropertyItem::ValueColumn, Qt::EditRole)); @@ -262,6 +274,9 @@ void DlgAddPropertyVarSet::addEditor(PropertyEditor::PropertyItem* propertyItem, QWidget::setTabOrder(ui->comboBoxType, editor.get()); QWidget::setTabOrder(editor.get(), ui->checkBoxAdd); + removeSelectionEditor(); + editor->blockSignals(false); + // FC_ERR("add editor"); // printFocusChain(editor.get()); } diff --git a/src/Gui/Dialogs/DlgAddPropertyVarSet.h b/src/Gui/Dialogs/DlgAddPropertyVarSet.h index d59a684cf6..3c797db686 100644 --- a/src/Gui/Dialogs/DlgAddPropertyVarSet.h +++ b/src/Gui/Dialogs/DlgAddPropertyVarSet.h @@ -92,6 +92,7 @@ private: void clearCurrentProperty(); void removeEditor(); + void removeSelectionEditor(); void addEditor(PropertyEditor::PropertyItem* propertyItem, std::string& type); bool isTypeWithEditor(const std::string& type);