diff options
author | Vladimir Belyavsky <belyavskyv@gmail.com> | 2023-12-11 21:45:22 +0300 |
---|---|---|
committer | Vladimir Belyavsky <belyavskyv@gmail.com> | 2024-01-09 08:50:50 +0300 |
commit | 694a677535ef11f9c966920c5e99d304bdbd94a5 (patch) | |
tree | 34ba408e6027dc176b7b4ef5f4912298b67e0868 /src/quick/items/qquicktextinput.cpp | |
parent | 4513eb4ec956a0363f1537359d7157456ac311bd (diff) |
TextArea/TextField: properly update placeholder text alignment
Placeholder text alignment depends on actual alignment of a parent
control, e.g. TextArea or TextField. Thus, if the horizontal text
alignment of the parent control is set explicitly, the placeholder
text must have the same alignment. Otherwise, the placeholder text
alignment should respect to the natural alignment of the text.
In order to do this QQuickPlaceholderText is connected to
effectiveHorizontalAlignmentChanged() signal of the parent control.
The problem is that the signal may not be emitted when alignment
is set explicitly after the component creation, so the placeholder
text alignment will not be updated respectively. To solve this we
need to make sure that the signal is forcibly emitted every time
when the alignment is set explicitly.
Fixes: QTBUG-120052
Pick-to: 6.7 6.6 6.5
Change-Id: Ib66a7a46d523777cc54ca6b6883d3fecc800dfb2
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quick/items/qquicktextinput.cpp')
-rw-r--r-- | src/quick/items/qquicktextinput.cpp | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index b5bb20d720..d0939d70d2 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -555,9 +555,8 @@ QQuickTextInput::HAlignment QQuickTextInput::hAlign() const void QQuickTextInput::setHAlign(HAlignment align) { Q_D(QQuickTextInput); - bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror; - d->hAlignImplicit = false; - if (d->setHAlign(align, forceAlign) && isComponentComplete()) { + + if (d->setHAlign(align, true) && isComponentComplete()) { d->updateLayout(); updateCursorRectangle(); } @@ -592,17 +591,34 @@ QQuickTextInput::HAlignment QQuickTextInput::effectiveHAlign() const return effectiveAlignment; } -bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment alignment, bool forceAlign) +bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment align, bool forceAlign) { Q_Q(QQuickTextInput); - if ((hAlign != alignment || forceAlign) && alignment <= QQuickTextInput::AlignHCenter) { // justify not supported - QQuickTextInput::HAlignment oldEffectiveHAlign = q->effectiveHAlign(); - hAlign = alignment; - emit q->horizontalAlignmentChanged(alignment); - if (oldEffectiveHAlign != q->effectiveHAlign()) - emit q->effectiveHorizontalAlignmentChanged(); + if (align > QQuickTextInput::AlignHCenter) + return false; // justify is not supported + + if (hAlign == align && !forceAlign) + return false; + + const bool wasImplicit = hAlignImplicit; + const auto oldEffectiveHAlign = q->effectiveHAlign(); + + hAlignImplicit = !forceAlign; + if (hAlign != align) { + hAlign = align; + emit q->horizontalAlignmentChanged(align); + } + + if (q->effectiveHAlign() != oldEffectiveHAlign) { + emit q->effectiveHorizontalAlignmentChanged(); return true; } + + if (forceAlign && wasImplicit) { + // QTBUG-120052 - when horizontal text alignment is set explicitly, + // we need notify any other controls that may depend on it, like QQuickPlaceholderText + emit q->effectiveHorizontalAlignmentChanged(); + } return false; } @@ -646,16 +662,19 @@ Qt::LayoutDirection QQuickTextInputPrivate::layoutDirection() const bool QQuickTextInputPrivate::determineHorizontalAlignment() { - if (hAlignImplicit) { - // if no explicit alignment has been set, follow the natural layout direction of the text - Qt::LayoutDirection direction = textDirection(); + if (!hAlignImplicit) + return false; + + // if no explicit alignment has been set, follow the natural layout direction of the text + Qt::LayoutDirection direction = textDirection(); #if QT_CONFIG(im) - if (direction == Qt::LayoutDirectionAuto) - direction = QGuiApplication::inputMethod()->inputDirection(); + if (direction == Qt::LayoutDirectionAuto) + direction = QGuiApplication::inputMethod()->inputDirection(); #endif - return setHAlign(direction == Qt::RightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft); - } - return false; + + const auto implicitHAlign = direction == Qt::RightToLeft ? + QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft; + return setHAlign(implicitHAlign); } QQuickTextInput::VAlignment QQuickTextInput::vAlign() const |