From b2aeac9891feb1e862d3f847a0695698efff8bbc Mon Sep 17 00:00:00 2001 From: Viktor Arvidsson Date: Fri, 25 Aug 2023 16:03:36 +0200 Subject: [PATCH] QTextFormat: Allow merging unset properties There was no way to merge removal of properties which is required in case you want to unset some property on the current selected text in a text document. For example in case your text has font size set in pixels and you want to change it to point size, that would be impossible before this change. With this change you can set the font point size to what you want and then set font pixel size to QVariant(). This is maybe not the most obvious way to do it but any other way I could think of would require new api and quite some code duplication. [ChangeLog][QtGui][Text] QTextFormat now allows setting properties to an invalid QVariant to allow clearing properties via a merge. This changes behavior slightly where code iterating over a QTextFormats properties would never encounter invalid variants, which is now possible if setProperty with an invalid variant was used instead of using the clearProperty function to remove properties. Change-Id: Ie8290088d457bf19d9c4785cfe91ae6a1de572c4 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/gui/text/qtextformat.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/gui/text/qtextformat.cpp b/src/gui/text/qtextformat.cpp index bbf89c764b..ce15389254 100644 --- a/src/gui/text/qtextformat.cpp +++ b/src/gui/text/qtextformat.cpp @@ -955,7 +955,11 @@ void QTextFormat::merge(const QTextFormat &other) p->props.reserve(p->props.size() + otherProps.size()); for (int i = 0; i < otherProps.size(); ++i) { const QT_PREPEND_NAMESPACE(Property) &prop = otherProps.at(i); - p->insertProperty(prop.key, prop.value); + if (prop.value.isValid()) { + p->insertProperty(prop.key, prop.value); + } else { + p->clearProperty(prop.key); + } } } @@ -1212,10 +1216,8 @@ void QTextFormat::setProperty(int propertyId, const QVariant &value) { if (!d) d = new QTextFormatPrivate; - if (!value.isValid()) - clearProperty(propertyId); - else - d->insertProperty(propertyId, value); + + d->insertProperty(propertyId, value); } /*!