“Cannot anchor to a null item” waring in QML

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
            }
        }
    ]
    ...
}

3 Responses to “Cannot anchor to a null item” waring in QML

  1. dmitriano says:

    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 }
    }

  2. dmitriano says:

    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...
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *