[SOLVED] Connections with null target
-
Hi,
I encountered some unexpected behaviour while dealing with the
Connections
object and itstarget
property. The documentation says:If set to null, no connection is made and any signal handlers are ignored until the target is not null.
Now have a look at the following snippet:
Item { property var databaseTable: null Connections { target: databaseTable onRowAppended: model.append(row) } }
This leads to the following runtime error:
QML Connections: Cannot assign to non-existent property "onRowAppended"
But the following produces no error:
Item { property var databaseTable: null Connections { target: databaseTable==null ? null : databaseTable onRowAppended: model.append(row) } }
Can anyone please give an explanation for this?
P.S.: I know there is
ignoreUnknownSignals: bool
, but that's not the point here. -
Interesting! Sounds like a bug, as I'd expect both to behave the same.
You can report this to https://bugreports.qt.io/
-
Interesting! Sounds like a bug, as I'd expect both to behave the same.
You can report this to https://bugreports.qt.io/
@JKSH Yes, looks like a bug to me, too, but maybe it's just another Javascript craziness?
-
@JKSH Yes, looks like a bug to me, too, but maybe it's just another Javascript craziness?
@Wieland: I'm not sure. See if anyone with more experience replies. If not, you could ask on the Interest mailing list where the Qt engineers hang out (you need to subscribe first)
-
@Wieland: I'm not sure. See if anyone with more experience replies. If not, you could ask on the Interest mailing list where the Qt engineers hang out (you need to subscribe first)
@JKSH Thanks for your help! I just figured out what's going wrong here. It's not a bug, but all my fault:
Item { property var databaseTable: null Connections { target: databaseTable // <- tries to access the *global* object "databaseTable" onRowAppended: model.append(row) } }
Change it to:
Item { id: myItem property var databaseTable: null Connections { target: myItem.databaseTable onRowAppended: model.append(row) } }
... and it accesses the intended local object.
So, the $500 question: Why the heck does
target: databaseTable==null ? null : databaseTable
not cause an error?Here is the answer: The situation in my actual code looks like this:
Item { id: someItemUpInTheHierarchy property var databaseTable // not initalized to *null*, so it is *undefined* // ... lots of code ... Item { property var databaseTable: null Connections { target: databaseTable==null ? null : databaseTable // undefined==null evaluates to *true* onRowAppended: model.append(row) } } }
Stupid me! But Javascript is stupid, too ;-)
-
@JKSH Thanks for your help! I just figured out what's going wrong here. It's not a bug, but all my fault:
Item { property var databaseTable: null Connections { target: databaseTable // <- tries to access the *global* object "databaseTable" onRowAppended: model.append(row) } }
Change it to:
Item { id: myItem property var databaseTable: null Connections { target: myItem.databaseTable onRowAppended: model.append(row) } }
... and it accesses the intended local object.
So, the $500 question: Why the heck does
target: databaseTable==null ? null : databaseTable
not cause an error?Here is the answer: The situation in my actual code looks like this:
Item { id: someItemUpInTheHierarchy property var databaseTable // not initalized to *null*, so it is *undefined* // ... lots of code ... Item { property var databaseTable: null Connections { target: databaseTable==null ? null : databaseTable // undefined==null evaluates to *true* onRowAppended: model.append(row) } } }
Stupid me! But Javascript is stupid, too ;-)
@Wieland said:
Stupid me! But Javascript is stupid, too ;-)
Stupidities make life more interesting ;-) Glad you've solved it! Thanks for sharing your story and solution.
P.S. This is one reason why I like to explicitly state the id (
id.propertyName
, or at leastparent.propertyName
) if the property doesn't belong to the current object. Another user had a similar predicament recently: https://forum.qt.io/topic/53997/ -
@JKSH Thanks for your help! I just figured out what's going wrong here. It's not a bug, but all my fault:
Item { property var databaseTable: null Connections { target: databaseTable // <- tries to access the *global* object "databaseTable" onRowAppended: model.append(row) } }
Change it to:
Item { id: myItem property var databaseTable: null Connections { target: myItem.databaseTable onRowAppended: model.append(row) } }
... and it accesses the intended local object.
So, the $500 question: Why the heck does
target: databaseTable==null ? null : databaseTable
not cause an error?Here is the answer: The situation in my actual code looks like this:
Item { id: someItemUpInTheHierarchy property var databaseTable // not initalized to *null*, so it is *undefined* // ... lots of code ... Item { property var databaseTable: null Connections { target: databaseTable==null ? null : databaseTable // undefined==null evaluates to *true* onRowAppended: model.append(row) } } }
Stupid me! But Javascript is stupid, too ;-)
Thanks, had similar bug with
target
being undefined:"Unable to assign [undefined] to QObject*"
Fixed according to the Qt doc:
https://doc.qt.io/qt-5/qml-qtqml-connections.html#target-propIf set to
null
, no connection is made and any signal handlers are ignored until the target is notnull
.Connections { // If set to null, no connection is made and // any signal handlers are ignored until the target is not null. target: myItem ? myItem : null onPropertyChanged: { ... } }