QObject: Do not register an observer when writing objectName property

Observers should only be registered when _reading_ the property.
Otherwise we get binding loops.

Change-Id: I974f6ea444fa7a5d333ed79eea6f34e3d757d169
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 9306db6d465a165ebe2edcbe5b0180f04d0c46ee)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ulf Hermann 2023-08-18 12:21:27 +02:00 committed by Qt Cherry-pick Bot
parent 2fd12c03cd
commit 3ce73dc0d6
2 changed files with 12 additions and 2 deletions

View File

@ -1320,7 +1320,7 @@ void QObject::doSetObjectName(const QString &name)
d->extraData->objectName.removeBindingUnlessInWrapper();
if (d->extraData->objectName != name) {
if (d->extraData->objectName.valueBypassingBindings() != name) {
d->extraData->objectName.setValueBypassingBindings(name);
d->extraData->objectName.notify(); // also emits a signal
}
@ -1338,7 +1338,7 @@ void QObject::setObjectName(QAnyStringView name)
d->extraData->objectName.removeBindingUnlessInWrapper();
if (d->extraData->objectName != name) {
if (d->extraData->objectName.valueBypassingBindings() != name) {
d->extraData->objectName.setValueBypassingBindings(name.toString());
d->extraData->objectName.notify(); // also emits a signal
}

View File

@ -8197,6 +8197,16 @@ void tst_QObject::objectNameBinding()
QObject obj;
QTestPrivate::testReadWritePropertyBasics<QObject, QString>(obj, "test1", "test2",
"objectName");
const QPropertyBinding<QString> binding([]() {
QObject obj2;
obj2.setObjectName(QLatin1String("no loop"));
return obj2.objectName();
}, {});
obj.bindableObjectName().setBinding(binding);
QCOMPARE(obj.objectName(), QLatin1String("no loop"));
QVERIFY2(!binding.error().hasError(), qPrintable(binding.error().description()));
}
namespace EmitToDestroyedClass {