QObject: Do not register an observer when writing objectName property

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

Pick-to: 6.6 6.5
Change-Id: I974f6ea444fa7a5d333ed79eea6f34e3d757d169
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Ulf Hermann 2023-08-18 12:21:27 +02:00
parent 7c9e61a3fe
commit 9306db6d46
2 changed files with 12 additions and 2 deletions

View File

@ -1321,7 +1321,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
}
@ -1339,7 +1339,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 {