Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Repeater child's parent null during destruction?
Forum Updated to NodeBB v4.3 + New Features

Repeater child's parent null during destruction?

Scheduled Pinned Locked Moved QML and Qt Quick
qml
3 Posts 2 Posters 1.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Edwin Vane
    wrote on last edited by
    #1

    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.

    p3c0P 1 Reply Last reply
    0
    • E Edwin Vane

      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.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Edwin Vane
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved