From 6a681cde67c4b5d4d56e87447b668746a284c384 Mon Sep 17 00:00:00 2001 From: Beat Hangartner Date: Mon, 5 Oct 2026 17:57:32 +0200 Subject: [PATCH] Keep an element editor text field in place when its width changes In the element editor, changing the width of a right-aligned or centred dynamic text field (with the width box) moved its aligned edge: the field always grew or shrank from its left edge, so a right-aligned field's right edge moved. PartDynamicTextField::setTextWidth() now keeps the point chosen by the alignment in place, with the prepareAlignment()/finishAlignment() pair setPlainText() and setFont() already use. Not while the field is read from the .elmt, whose saved position is the one to keep. No file format change. Co-Authored-By: Claude Opus 5.5 --- .../editor/graphicspart/partdynamictextfield.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sources/editor/graphicspart/partdynamictextfield.cpp b/sources/editor/graphicspart/partdynamictextfield.cpp index 027a07ac4..958dcd0b8 100644 --- a/sources/editor/graphicspart/partdynamictextfield.cpp +++ b/sources/editor/graphicspart/partdynamictextfield.cpp @@ -235,7 +235,10 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt) { m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString())); setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false); + //The saved position is the one to keep + m_block_alignment = true; setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble()); + m_block_alignment = false; QMetaEnum me = DynamicElementTextItem::textFromMetaEnum(); m_text_from = DynamicElementTextItem::TextFrom( @@ -431,13 +434,23 @@ bool PartDynamicTextField::frame() const return m_frame; } +/** + @brief PartDynamicTextField::setTextWidth + Set the width of this text (-1 = automatic width). The point of the text + chosen by its alignment stays in place, as when the text changes: a + right-aligned or centred field no longer grows to the right only, and + undoing a width change puts it back where it was. + @param width +*/ void PartDynamicTextField::setTextWidth(qreal width) { + prepareAlignment(); this -> document() -> setTextWidth(width); //Adjust the width, to ideal width if needed if(width > 0 && document() -> size().width() > width) document() -> setTextWidth(document() -> idealWidth()); + finishAlignment(); m_text_width = document() -> textWidth(); emit textWidthChanged(m_text_width); }