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

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

Leave a Reply

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