Binding based on typeof doesn't work any more
-
Hi,
I have a simple Label that has to show a property of a rootProperty, the latter not being created when the qml is loaded/created. Quite frequently I use this construct and it did work perfectly in older Qt versions (something before 12.1 - that's the earliest I have installed)://[main.qml, inside a Label] text: typeof Controller === "undefined" ? "" : Controller.successfulSteps
Now this doesn't show anything anymore. The following workaround does show the desired result:
property string dummy: "" text: dummy+(typeof Controller === "undefined" ? "" : Controller.successfulSteps)
This seems very hackish. Is there a better way to achieve this? Why has the behavior been changed at all in the first place?
The Controller has been set as a root property after loading the QML page://[main.cpp] QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); Controller controller(&engine); engine.rootContext()->setContextProperty("Controller", QVariant::fromValue<Controller*>(&controller));
BR
Sebastian -
@SeDi said in Binding based on typeof doesn't work any more:
Quite frequently I use this construct and it did work perfectly in older Qt versions
...
Now this doesn't show anything anymore.
...
This seems very hackish. Is there a better way to achieve this? Why has the behavior been changed at all in the first place?
Hi Sebastian,
I'm not sure if the change was intended or if it's a bug in newer versions. The Qt engineers in the Interest mailing list (https://lists.qt-project.org/listinfo/interest ) could provide some insights or provide fix -- subscribe to the list and ask there.
-
@SeDi said in Binding based on typeof doesn't work any more:
This seems very hackish. Is there a better way to achieve this?
Why just not to write
text: Controller ? Controller.successfulSteps : ""
-
@IntruderExcluder : Thanks for the idea. I can't, because this leads to the qml warning/error
ReferenceError: Controller is not defined
as long this (Controller not yet defined) is the case, as undefined does not mean Null. Plus it also doesn't show a result.
@JKSH : Thanks, I've done that now! I'll come back with the final answer.
-
Than another ugly variant, but without dummy property:
text: { try { return Controller.successfulSteps; } catch (e) { return ""; } }
-
Thanks for your suggestions!
Unfortunately it's still either no binding or error message, if I don't use a dummy property. [At first I had though this to work, but that was a mistake on my side]
But by now I know the problem. It is actually a bug that was introduced with 5.11->5.12 and has been fixed in 5.12.5 / 5.13.1.
I've had tested it with 5.12.3 and 13.0 :-)Here's the bug:
https://bugreports.qt.io/browse/QTBUG-76796
Here's the fix:
https://codereview.qt-project.org/c/qt/qtdeclarative/+/266776The best solution, of course, would be to load the QML page after the contextProperty is set to the engine. I can and will do that with my current and future code.
For my legacy code this bug would have meant a huge amount of work - or an almost equal amount of (ugly) patching. I am very happy, that this bug has been fixed! I will just upgrade.
Thank you for time, effort and creative input!