From bb7ef72b3533787528a2bf40c27dd439ce47c5ff Mon Sep 17 00:00:00 2001 From: Pieter Dewachter Date: Tue, 10 Dec 2024 11:01:13 +0100 Subject: [PATCH 1/3] Make sure to clear focus of any widgets contained by an Expander to avoid visual artifarcts --- .../oclero/qlementine/widgets/Expander.hpp | 1 + lib/src/widgets/Expander.cpp | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) mode change 100644 => 100755 lib/include/oclero/qlementine/widgets/Expander.hpp diff --git a/lib/include/oclero/qlementine/widgets/Expander.hpp b/lib/include/oclero/qlementine/widgets/Expander.hpp old mode 100644 new mode 100755 index 4d061e9..a37ce4b --- a/lib/include/oclero/qlementine/widgets/Expander.hpp +++ b/lib/include/oclero/qlementine/widgets/Expander.hpp @@ -52,5 +52,6 @@ class Expander : public QWidget { Qt::Orientation _orientation{ Qt::Orientation::Vertical }; QVariantAnimation _animation; QPointer _content{ nullptr }; + QHash _focusPolicies; }; } // namespace oclero::qlementine diff --git a/lib/src/widgets/Expander.cpp b/lib/src/widgets/Expander.cpp index db6882b..ddeaf1e 100644 --- a/lib/src/widgets/Expander.cpp +++ b/lib/src/widgets/Expander.cpp @@ -109,6 +109,20 @@ void Expander::setExpanded(bool expanded) { emit aboutToShrink(); } + for (auto* widget : findChildren()) { + if (!_expanded) { + _focusPolicies.insert(widget, widget->focusPolicy()); + widget->setFocusPolicy(Qt::NoFocus); + widget->clearFocus(); + } else if (_focusPolicies.contains(widget)) { + widget->setFocusPolicy(_focusPolicies[widget]); + } + } + + if (_expanded) { + _focusPolicies.clear(); + } + const auto isVertical = _orientation == Qt::Orientation::Vertical; const auto current = isVertical ? height() : width(); const auto contentSizeHint = _content->sizeHint(); @@ -155,6 +169,7 @@ void Expander::setContent(QWidget* content) { if (content != _content) { if (_content) { _content->removeEventFilter(this); + _focusPolicies.clear(); delete _content; } @@ -169,6 +184,14 @@ void Expander::setContent(QWidget* content) { _content->setParent(this); _content->installEventFilter(this); _content->setVisible(_expanded); + + if (!_expanded) { + for (auto* widget : findChildren()) { + _focusPolicies.insert(widget, widget->focusPolicy()); + widget->setFocusPolicy(Qt::NoFocus); + widget->clearFocus(); + } + } } updateGeometry(); emit contentChanged(); From 5cb32e84c3e73b3b8e19a7bfebbcc3d6e22c8cf2 Mon Sep 17 00:00:00 2001 From: Pieter Dewachter Date: Fri, 17 Jan 2025 10:30:11 +0100 Subject: [PATCH 2/3] Revert "Make sure to clear focus of any widgets contained by an Expander to avoid visual artifarcts" This reverts commit bb7ef72b3533787528a2bf40c27dd439ce47c5ff. --- .../oclero/qlementine/widgets/Expander.hpp | 1 - lib/src/widgets/Expander.cpp | 23 ------------------- 2 files changed, 24 deletions(-) mode change 100755 => 100644 lib/include/oclero/qlementine/widgets/Expander.hpp diff --git a/lib/include/oclero/qlementine/widgets/Expander.hpp b/lib/include/oclero/qlementine/widgets/Expander.hpp old mode 100755 new mode 100644 index a37ce4b..4d061e9 --- a/lib/include/oclero/qlementine/widgets/Expander.hpp +++ b/lib/include/oclero/qlementine/widgets/Expander.hpp @@ -52,6 +52,5 @@ class Expander : public QWidget { Qt::Orientation _orientation{ Qt::Orientation::Vertical }; QVariantAnimation _animation; QPointer _content{ nullptr }; - QHash _focusPolicies; }; } // namespace oclero::qlementine diff --git a/lib/src/widgets/Expander.cpp b/lib/src/widgets/Expander.cpp index ddeaf1e..db6882b 100644 --- a/lib/src/widgets/Expander.cpp +++ b/lib/src/widgets/Expander.cpp @@ -109,20 +109,6 @@ void Expander::setExpanded(bool expanded) { emit aboutToShrink(); } - for (auto* widget : findChildren()) { - if (!_expanded) { - _focusPolicies.insert(widget, widget->focusPolicy()); - widget->setFocusPolicy(Qt::NoFocus); - widget->clearFocus(); - } else if (_focusPolicies.contains(widget)) { - widget->setFocusPolicy(_focusPolicies[widget]); - } - } - - if (_expanded) { - _focusPolicies.clear(); - } - const auto isVertical = _orientation == Qt::Orientation::Vertical; const auto current = isVertical ? height() : width(); const auto contentSizeHint = _content->sizeHint(); @@ -169,7 +155,6 @@ void Expander::setContent(QWidget* content) { if (content != _content) { if (_content) { _content->removeEventFilter(this); - _focusPolicies.clear(); delete _content; } @@ -184,14 +169,6 @@ void Expander::setContent(QWidget* content) { _content->setParent(this); _content->installEventFilter(this); _content->setVisible(_expanded); - - if (!_expanded) { - for (auto* widget : findChildren()) { - _focusPolicies.insert(widget, widget->focusPolicy()); - widget->setFocusPolicy(Qt::NoFocus); - widget->clearFocus(); - } - } } updateGeometry(); emit contentChanged(); From fa71b7800b900735e2caef790bb65fa728cb6ebe Mon Sep 17 00:00:00 2001 From: Pieter Dewachter Date: Fri, 17 Jan 2025 10:41:18 +0100 Subject: [PATCH 3/3] Rework implementation as suggested by oclero --- lib/include/oclero/qlementine/utils/WidgetUtils.hpp | 2 ++ lib/src/utils/WidgetUtils.cpp | 13 +++++++++++++ lib/src/widgets/Expander.cpp | 2 ++ 3 files changed, 17 insertions(+) diff --git a/lib/include/oclero/qlementine/utils/WidgetUtils.hpp b/lib/include/oclero/qlementine/utils/WidgetUtils.hpp index ae579b9..bc22c20 100644 --- a/lib/include/oclero/qlementine/utils/WidgetUtils.hpp +++ b/lib/include/oclero/qlementine/utils/WidgetUtils.hpp @@ -17,6 +17,8 @@ qreal getDpi(const QWidget* widget); QWindow* getWindow(const QWidget* widget); +void clearFocus(QWidget* widget, bool recursive); + template T* findFirstParentOfType(QWidget* child) { auto* parent = child; diff --git a/lib/src/utils/WidgetUtils.cpp b/lib/src/utils/WidgetUtils.cpp index 01bfa7d..80d0670 100644 --- a/lib/src/utils/WidgetUtils.cpp +++ b/lib/src/utils/WidgetUtils.cpp @@ -78,4 +78,17 @@ QWindow* getWindow(const QWidget* widget) { } return nullptr; } + +void clearFocus(QWidget* widget, bool recursive) { + if (widget) { + widget->clearFocus(); + + if (recursive) { + const auto children = widget->findChildren(); + for (auto* child : children) { + clearFocus(child, recursive); + } + } + } +} } // namespace oclero::qlementine diff --git a/lib/src/widgets/Expander.cpp b/lib/src/widgets/Expander.cpp index db6882b..2a0e18c 100644 --- a/lib/src/widgets/Expander.cpp +++ b/lib/src/widgets/Expander.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: MIT #include +#include #include #include @@ -106,6 +107,7 @@ void Expander::setExpanded(bool expanded) { if (_expanded) { emit aboutToExpand(); } else { + oclero::qlementine::clearFocus(this, true); emit aboutToShrink(); }