The following QML code
Callout
{
id: call
//margins should bound but not assigned
anchors.topMargin: root.above ? undefined : yMargin
anchors.bottomMargin: root.above ? yMargin : undefined
...
}
produced “Cannot anchor to a null item” warning and worked incorrectly.
I tried to replace it with AnchorChanges
and PropertyChanges
:
Callout
{
id: call
states: State
{
name: "under"
when: !root.above
AnchorChanges
{
target: call
anchors.top: parent.top
anchors.bottom: undefined
}
PropertyChanges
{
target: call
anchors.topMargin: yMargin
anchors.bottomMargin: undefined
}
}
anchors.top: undefined
anchors.bottom: parent.bottom
anchors.topMargin: undefined
anchors.bottomMargin: yMargin
...
}
and the code started to work correctly but the warning persisted and the following did not help:
Callout
{
id: call
states:
[
State
{
name: "above"
when: root.above
AnchorChanges
{
target: call
anchors.top: undefined
anchors.bottom: parent.bottom
}
PropertyChanges
{
target: call
//anchors.topMargin: undefined
anchors.bottomMargin: yMargin
}
},
State
{
name: "under"
when: !root.above
AnchorChanges
{
target: call
anchors.top: parent.top
anchors.bottom: undefined
}
PropertyChanges
{
target: call
anchors.topMargin: yMargin
//anchors.bottomMargin: undefined
}
}
]
...
}
https://doc.qt.io/qt-5/qml-qtquick-propertychanges.html
states: State {
name: "resized"; when: mouseArea.pressed
PropertyChanges { target: rect; color: "blue"; height: container.height }
}
Receiving ‘Cannot anchor to a null item’ when using States
https://stackoverflow.com/questions/71768533/receiving-cannot-anchor-to-a-null-item-when-using-states
https://doc.qt.io/qt-6/qml-qtqml-qt.html#platform-prop
https://stackoverflow.com/questions/39552072/text-in-qml-rendered-different-accross-platforms
property real offset: {
switch (Qt.platform.os) {
case "android": return androidValue
case "windows": return windowsValue
// and so on...
}
}