Repeater child's parent null during destruction?



  • Consider the following QML:

            Rectangle {
                anchors.fill: parent
                Repeater {
                    model: viewModel.children
                    Child {
                        width: viewModel.childSize 
                        height: viewModel.childSize
                        centreX: modelData.x * parent.width
                        centreY: modelData.y * parent.height
                    }
                }
            }
    

    The problem comes when the model changes to an empty list. For some reason the bindings for the existing items, which are about to be deleted, are reevaluated but parent is null. I get TypeError: Cannot read property of null errors on the console as a result. I've eliminated the possibility of modelData being the null object. I can avoid this with the ternary operator (i.e. centreX: parent ? modelData.x * parent.width : 0) but this seems a hacky workaround. Is what I'm seeing expected behaviour? Am I stepping on some restriction with property bindings? Any insight would be appreciated.


  • Moderators

    Hi @Edwin-Vane

    I can avoid this with the ternary operator (i.e. centreX: parent ? modelData.x * parent.width : 0) but this seems a hacky workaround.

    IMO, it is the proper way. Even official Qt examples have done in the same way. You can see an example here. See under ColumnLayout component.



  • That example sometimes uses parent properties directly and sometimes with protection. What's the rule we should apply regarding whether to use null protection or not?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.